473,385 Members | 1,402 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,385 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 9389

"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) ----------
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.