auto_ptr and copy ctr question

S

Soumen

If I've class with one of data members of auto_ptr type, how should I
write copy ctr or copy assignment operator if I don't intend to allow
transfer of ownership happen? Declaring that member const could be one
option I guess. Clarify me if this understanding is wrong. Do I need
to disable these (copy ctr and assignment operator) in such cases?
What happens when I don't write one and use the compiler generated
one? Does transfer of ownership still happens or it's same as
declaring member as const? Looks like disabling these two are best
design decision.

Regards,
~ Soumen
 
S

Soumen

If you don't provide the "move semantics" offered by the auto_ptr
itself, you could do two things. One, as you mentioned, is to prohibit
the copying altogether. That's a bit harsh to me, but if your design
can endure that, it's the simplest of the two. The other is to provide
copying of the owned object by copy-creating a new one in case of the
constructor and copy-assigning the pointed-to item in case of the
assignment:

class Has_auto_ptr {
std::auto_ptr<sometype> myPtr;
public:
...
Has_auto_ptr(Has_auto_ptr const& other) :
myPtr(new sometype(*other.myPtr)) {}
Has_auto_ptr& operator =(Has_auto_ptr const& other) {
*myPtr = *other.myPtr;
return *this;
}
};

Disclaimer: the code is unchecked, provided for illustration purposes only.

V

Thanks. Yes, deep copy is always an option.

The reason for not allowing transfer of ownership is original
object's data can potentially become invalid if copied object
goes out of scope.

Is there any better standard smart pointer which does some sort
of ref counting such that I can allow copy of pointed address
and still not bother of freeing the resource or pointer becoming
invalid?

Regards,
~ Soumen
 
J

James Kanze

If I've class with one of data members of auto_ptr type, how
should I write copy ctr or copy assignment operator if I don't
intend to allow transfer of ownership happen? Declaring that
member const could be one option I guess. Clarify me if this
understanding is wrong. Do I need to disable these (copy ctr
and assignment operator) in such cases? What happens when I
don't write one and use the compiler generated one? Does
transfer of ownership still happens or it's same as declaring
member as const? Looks like disabling these two are best
design decision.

It depends on the desired semantics. If transfer of ownership
is not desired, you have two choices: don't support copy (by
declaring the copy constructor private, and not providing an
implementation), or write a copy constructor which does a deep
copy.
 
T

Triple-DES

Is there any better standard smart pointer which does some sort
of ref counting such that I can allow copy of pointed address
and still not bother of freeing the resource or pointer becoming
invalid?

Regards,
~ Soumen

boost/tr1 :: shared_ptr is widely used, however it is not standard
yet.

DP
 
S

Soumen

boost/tr1 :: shared_ptr is widely used, however it is not standard
yet.

DP

Thanks. Though not part of standard, if I use boost::shared_ptr,
boost::shared_array,
boost::weak_ptr etc. properly, will the code be portable to other
platform?

Regards,
~ Soumen
 
D

dizzy

Soumen said:
Thanks. Though not part of standard, if I use boost::shared_ptr,
boost::shared_array,
boost::weak_ptr etc. properly, will the code be portable to other
platform?

It will work on all the platforms that boost supports, which are plenty. It
will work on some others too if we assume shared_ptr is using just a small
part of the C++ features that boost requires in general.
 
S

Soumen

It will work on all the platforms that boost supports, which are plenty. It
will work on some others too if we assume shared_ptr is using just a small
part of the C++ features that boost requires in general.

Thanks. Also, could you please give an idea about the overhead of this
additional
book-keeping on efficiency?

Regards,
~ Soumen
 
S

Soumen

Soumen said:
[..]
Thanks. Also, could you please give an idea about the overhead of this
additional
book-keeping on efficiency?

Additional bookkeeping lowers the performance.

Oh... Did you want numbers? Sorry, no numbers. Put it in, profile,
and if you find it to be the bottleneck, deal with it then and there,
not before even attempting to use it.

V

Thanks for reminding "Don't do pre-mature optimization".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top