473,698 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9448

"alg" <al******@yahoo .com> wrote in message
news:85******** *************@b gtnsc04-news.ops.worldn et.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>(n ew Cat(6));

Jul 19 '05 #2

"alg" <al******@yahoo .com> wrote in message
news:85******** *************@b gtnsc04-news.ops.worldn et.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<C at> 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.worldn et.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<typena me 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::loud ly);
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<Ca t>(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
4079
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> #include <string>
4
3545
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
3489
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 assignment from temporaries. I have placed the manuscript at: http://www.nd.edu/~ahenrick/auto_ptr.pdf It develops the problem and its solution in an incremental manner, which will hopefully make it accessible for most people without being
2
4572
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 following (in file testInteger.cc): line 38: std::auto_ptr<HInteger> d(new HInteger(5)); line 39: std::auto_ptr<HObject> e(new HObject());
10
2624
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 section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
9
2916
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 behaviour of std::auto_ptr. 2. To implement a pkt::auto_ptr without auto_ptr_ref. 3. Check out the limitations of pkt::auto_ptr as compared to std::auto_ptr (using steps 1 and 2).
39
864
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 Query("123");
10
3816
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
2446
by: Barry | last post by:
struct A { void Print() const { cout << "Print" << endl; } }; auto_ptr<AGet() {
17
10066
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
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.