473,405 Members | 2,287 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,405 software developers and data experts.

auto_ptr

alg
I am learning to use "auto_ptr" and found that the following 2 line would
not compile:

auto_ptr<Cat> pCat;
pCat = new Cat(6);

until I changed it to the one:

auto_ptr<Cat> pCat(new Cat(6));

Anf the error is:

"error C2679: binary '=' : no operator defined which takes a right-hand
operand of type 'class Cat *' (or there is no acceptable conversion)"

Could you please give me some reasons why such an error?

Thanks for your help!
Jul 19 '05 #1
4 9394

"alg" <al******@yahoo.com> wrote in message
news:85*********************@bgtnsc04-news.ops.worldnet.att.net...
I am learning to use "auto_ptr" and found that the following 2 line would
not compile:

auto_ptr<Cat> pCat;
pCat = new Cat(6);

until I changed it to the one:

auto_ptr<Cat> pCat(new Cat(6));

Anf the error is:

"error C2679: binary '=' : no operator defined which takes a right-hand
operand of type 'class Cat *' (or there is no acceptable conversion)"

Could you please give me some reasons why such an error?

Thanks for your help!


auto_ptr doesn't allow implicit conversion from plain pointer.
It'll work if you try this:

auto_ptr<Cat> pCat;
pCat = auto_ptr<Cat>(new Cat(6));

Jul 19 '05 #2

"alg" <al******@yahoo.com> wrote in message
news:85*********************@bgtnsc04-news.ops.worldnet.att.net...
| I am learning to use "auto_ptr" and found that the following 2 line would
| not compile:
|
| auto_ptr<Cat> pCat;
| pCat = new Cat(6);
|
| until I changed it to the one:
|
| auto_ptr<Cat> pCat(new Cat(6));
|
| Anf the error is:
|
| "error C2679: binary '=' : no operator defined which takes a right-hand
| operand of type 'class Cat *' (or there is no acceptable conversion)"
|
| Could you please give me some reasons why such an error?

The following...

# include <memory>
std::auto_ptr<Cat> pCat( new Cat( 6 ) );

....should work fine.

Did you include the correct header ?

Cheers.
Chris Val


Jul 19 '05 #3
alg wrote:
I am learning to use "auto_ptr" and found that the following 2 line would
not compile:

auto_ptr<Cat> pCat;
pCat = new Cat(6);

until I changed it to the one:

auto_ptr<Cat> pCat(new Cat(6));

Anf the error is:

"error C2679: binary '=' : no operator defined which takes a right-hand
operand of type 'class Cat *' (or there is no acceptable conversion)"

Could you please give me some reasons why such an error?
...


The compiler has already given you the reason. 'std::auto_ptr' cannot be
assigned from an ordinary pointer since there is no way to convert this
pointer to any of the types 'std::auto_ptr's assignment operators will
accept.

You can use either the 'std::auto_ptr's constructor or, if you really
need to change the store pointer value afterwards, 'std::auto_ptr's
method 'reset'.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #4
"alg" <al******@yahoo.com> wrote in message news:<85*********************@bgtnsc04-news.ops.worldnet.att.net>...
I am learning to use "auto_ptr" and found that the following 2 line would
not compile:

auto_ptr<Cat> pCat;
pCat = new Cat(6);

until I changed it to the one:

auto_ptr<Cat> pCat(new Cat(6));


People have already given the correct reason (there is no implicit
conversion from T* to auto_ptr<T>); I will give you the reason as
there are some people here who would be glad to see it I think. I know
that a month ago (before reading Josuttis) I would have. So anyway:

Because auto_ptr is a smart pointer, it will take "ownership" of the
pointer. This is what it's made for, therefor it is usually a good
thing. However, sometimes it can be easy to give an auto_ptr ownership
when you don't want to or don't realize it.

You'll notice that the following also will (should) not compile:
auto_ptr<Cat> pCat = new Cat(6);
Note that this is different than the two examples you use above,
especially the first. A statement such as the above will call a
one-argument constructor, not the default constructor and then an
assignment. Thus the above call is *almost* the same as
auto_ptr<Cat> pCat(new Cat(6));
The only difference is that in the first example there is an implicit
conversion from Cat* to auto_ptr<Cat>, while the second example has an
explicit conversions.

Normally implicit conversions are well and good, for instance it's
very nice to be able to say
string s = "Hello world!";
which is an implicit conversion from const char* to string. As another
example,
double abs(double arg)
{ return (arg>0) ? arg : -arg; }

c = abs(14);
is also an implicit conversion from an integer (14) to a double. (For
the record, I don't know if that's the actual signature of a standard
version of abs, but we'll go with it at least.) It'd be kind of a pain
to have to write
c = abs(double(14));
instead. (Actually, 14.0 would be better there, but that would of
course not work with variables.) However, implicit conversions can be
a source of problems in certain cases, which comes up a lot with
auto_ptrs.

For instance, consider what would happen if you pass a smart pointer
to a function:
template<typename T>
void foo(auto_ptr<T> uhoh)
{ }
the argument uhoh takes posession of the passed object which is then
destroyed when the function exits. Meanwhile your client code
Cat *c = new Cat;
foo(c);
calls foo, perhaps not realzing that foo takes an auto_ptr, or not
realizing the semantics of auto_ptr, or you think you're calling a
different function, or whatever. In any case, a temporary
auto_ptr<Cat> is created that points to *c, it's passed to the
function which takes ownership of it in the form of uhoh, the function
goes out of scope, uhoh is destructed which deletes the Cat object,
and the function returns. You then go to try to use c some more
c->Purr(Cat::loudly);
but the Cat c points to is now deleted! (I'm not sure if auto_ptr sets
the pointer to null; a quick look at Josuttis seems to indicate it
doesn't, but I'm not sure.)

What is much better is for implicit conversions to be ruled out. The
way this is done is to specify the constructor as explicit, as in
explicit auto_ptr(T* ptr = 0);
(this is the actual relivant constructor according to Josuttis minus
the throw();). The explicit keyword gurantees that mistakes like the
above con't be made. It requires
auto_ptr<Cat> apc(new Cat(9));
Cat *c = new Cat(9);
foo(auto_ptr<Cat>(c)); // note the explicit cast
instead of
auto_ptr<Cat> apc = new Cat(9);
Cat *c = new Cat(9);
foo(c); // note the explicit cast

While from what I've seen it seems that most of the problems could
come from one-argument constructors being called implicitly, there are
I'm sure similar problems with assignment operators allowing
assignment of T* directly, so this is why it isn't allowed.
Jul 19 '05 #5

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

Similar topics

5
by: gg | last post by:
I am getting the following compilation errors with the following program. My compiler is aCC 03.27 on HP-UX11 - #include <iostream> using namespace std; #include <list> #include <memory>...
4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
14
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and...
2
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
9
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
10
by: mosfet | last post by:
Hi, Let's say I have a vector of auto_ptr defined like this : vector< auto_ptr<T v; is it allowed ? Is there any side effects ? If it's not a good idea, how can I fix this ?
18
by: Barry | last post by:
struct A { void Print() const { cout << "Print" << endl; } }; auto_ptr<AGet() {
17
by: Ankur Arora | last post by:
Hi All, I'm building a sample application that uses a custom auto_ptr implementation. The program crashes with the following output:- Output (Debug Assertion failed) ----------
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.