Jump to content
  • Advertisement
Sign in to follow this  
Gink

Why does ownership transfer(auto ptr)

This topic is 4721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ownership transfers in this statement
template<class U> auto_ptr::auto_ptr (auto_ptr<U>& ap) throw()
but not this one?
ostream& operator<< (ostream& strm,auto_ptr<T> & p)

Share this post


Link to post
Share on other sites
Advertisement
Because it's passed by reference in the second case, so it's copy constructor is never called.

Share this post


Link to post
Share on other sites
Its passed by reference in each example. An auto ptr enforeces only one holder of the underlying pointer and therefor only gets invalidated when it assigns the underlying ptr to another pointer.

It does this in the first example but not the second.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
ownership transfers in this statement

template<class U> auto_ptr::auto_ptr (auto_ptr<U>& ap) throw()

but not this one?

ostream& operator<< (ostream& strm,auto_ptr<T> & p)


The goal of operator<< is not to take ownership of the auto_ptr but to serialize it. Therefore, ownership transfer is not needed.

Declaring a parameter don't embed much semantics in the declaration. The semantic of a function is defined by the function goal, not by its parameters. I don't know whether I'm clear of not. To simplify: a particular code construction - here, the auto_ptr parameter - can be used for different purpose, and thus behave differently.

HTH,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!