473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Smart resource manager

I am trying to create an automatic resource manager that works very much
the same as std::auto_ptr. However, I have a compile error I cannot
figure out. The following program demonstrates the problem. Pretend,
in each case, that -1 means "no resource", and any other number is a
handle to a valid resource. The class releases the resource in the
destructor (similar to a std::auto_ptr calling delete), so any time a
copy of the class is made, ownership of the resource should be transferred.

class A
{
int s ;
public :
A() : s(-1)
{ }

A(int s) : s(s)
{ }

A(A &a) : s(a.s)
{
a.s = -1 ;
}

~A() throw()
{
// Do something to release resource.
s = -1 ;
}

A &operator=(A &a) throw()
{
s = a.s ;
a.s = -1 ;
return *this ;
}
} ;

A f()
{
A a(5) ;
return a ;
}

int main()
{
A a ;
a = f() ;
return 0 ;
}
Compling gives the following problem:
$ g++ -W -Wall -ansi -pedantic test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: no match for 'operator=' in 'a = f()()'
test.cpp:25: error: candidates are: A& A::operator=(A&)

That is, the assignment fails, for some reason I cannot determine. Any
insight would be appreciated.

Thanks,
Alan
Jul 22 '05 #1
2 2059
> A(int s) : s(s)

dont use "s" as your paramter name, use something other then the name of
your data member
likely, this is your problem.

-Chris
Jul 22 '05 #2
Alan Johnson wrote:
I am trying to create an automatic resource manager that works very
much
the same as std::auto_ptr. However, I have a compile error I cannot
figure out. The following program demonstrates the problem. Pretend,
in each case, that -1 means "no resource", and any other number is a
handle to a valid resource. The class releases the resource in the
destructor (similar to a std::auto_ptr calling delete), so any time a
copy of the class is made, ownership of the resource should be
transferred.

class A
{
int s ;
public :
A() : s(-1)
{ }

A(int s) : s(s)
{ }

A(A &a) : s(a.s)
{
a.s = -1 ;
}

~A() throw()
{
// Do something to release resource.
s = -1 ;
}

A &operator=(A &a) throw()
{
s = a.s ;
a.s = -1 ;
return *this ;
}
} ;

A f()
{
A a(5) ;
return a ;
}

int main()
{
A a ;
a = f() ;
return 0 ;
}
Compling gives the following problem:
$ g++ -W -Wall -ansi -pedantic test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: no match for 'operator=' in 'a = f()()'
test.cpp:25: error: candidates are: A& A::operator=(A&)

That is, the assignment fails, for some reason I cannot determine.
Any insight would be appreciated.


Your programm is not const-correct. f() returns a temporary, and in C++,
you can't bind a non-const reference to a temporary. therefore, no
suitable operator= is found. Since your operator= actually needs to
modify the s member of the right hand side, you'd have to use the
mutable keyword for your member. Try this:

class A
{
mutable int s ;
public :
A() : s(-1)
{ }

A(int s) : s(s)
{ }

A(const A &a) : s(a.s)
{
a.s = -1 ;
}

~A() throw()
{
// Do something to release resource.
s = -1 ;
}

A &operator=(const A &a) throw()
{
s = a.s ;
a.s = -1 ;
return *this ;
}
} ;

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
5
by: Martin Bischoff | last post by:
Hi, is it possible to implement custom resource managers for ASP.NET 2.0 so that strings can be read from a database (for example)? Ideally, it should be possible to configure the custom...
5
by: Boris | last post by:
I've a class C with a smart pointer (I use boost::shared_ptr) which is initialized in the constructor: class C { boost::shared_ptr<D> d; public: C() : d(new d()) { } }; When the program...
1
by: Tom | last post by:
This may seem a little strange, but in all my years of developing Windows Forms apps, I've never really used Resource files; however, I would like to start doing so in order to store icons,...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
0
by: ramesh.nrk | last post by:
Hi, I am developing a windows application which should support different language for this I am using "Resource Manager" default in .net framework. Here I am developing dll for Resource because...
0
by: =?Utf-8?B?QWlyYm9ybmU4Mg==?= | last post by:
I'm getting an Error 1068 Dependency group or service failed to start. I've checked on the Smart Card Resource Manager and the service is set to automatic, all other dependencies are also running....
0
harshadd
by: harshadd | last post by:
Dear all, I am trying to install File server Resource Manager to apply user level directory quota. Tried to use this link for help , it is as below: I checked the GIF file But on my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.