473,385 Members | 1,317 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 and copy ctr question

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
Jun 27 '08 #1
9 1788
Soumen wrote:
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.
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<sometypemyPtr;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On May 29, 6:24 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Soumen wrote:
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.

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<sometypemyPtr;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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
Jun 27 '08 #3
On May 29, 3:16 pm, Soumen <soume...@gmail.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
On 29 Mai, 15:52, Soumen <soume...@gmail.comwrote:
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
Jun 27 '08 #5
On May 30, 10:33 am, Triple-DES <DenPlettf...@gmail.comwrote:
On 29 Mai, 15:52, Soumen <soume...@gmail.comwrote:
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
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
Jun 27 '08 #6
Soumen wrote:
On May 30, 10:33 am, Triple-DES <DenPlettf...@gmail.comwrote:
>On 29 Mai, 15:52, Soumen <soume...@gmail.comwrote:
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

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.

--
Dizzy

Jun 27 '08 #7
On May 30, 1:54 pm, dizzy <di...@roedu.netwrote:
Soumen wrote:
On May 30, 10:33 am, Triple-DES <DenPlettf...@gmail.comwrote:
On 29 Mai, 15:52, Soumen <soume...@gmail.comwrote:
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
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.

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

Regards,
~ Soumen
Jun 27 '08 #8
Soumen wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #9
On Jun 2, 6:01 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Soumen wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for reminding "Don't do pre-mature optimization".
Jun 27 '08 #10

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

Similar topics

6
by: Noah Roberts | last post by:
Often I have a method which accepts a pointer to some class as an argument. Usually that pointer is managed by the caller through std::auto_ptr. I would assume that you could pass the auto_ptr in...
20
by: Bronek Kozicki | last post by:
Hi Please try this code. I think that it's perfectly legal. However when compiled under MSVC71 stack overflow happens in first line of main, thus second line is never executed. B.
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: flopbucket | last post by:
I saw the following post a few messages back and am trying to understand exactly why this is so. Thanks for the information. My comments begin with **. The original post was arguing that STL...
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 ...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
2
by: dolphin | last post by:
Hi All Today , I read the source code of auto_ptr. I have a question about the copy constructor of auto_ptr. template<class _Ty> class auto_ptr { .................. } there is a copy...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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
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?
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...

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.