473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1816
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<s ometypemyPtr;
public:
...
Has_auto_ptr(Ha s_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...@com Acast.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<s ometypemyPtr;
public:
...
Has_auto_ptr(Ha s_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 objektorientier ter Datenverarbeitu ng
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...@g mail.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_p tr,
boost::shared_a rray,
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...@g mail.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_p tr,
boost::shared_a rray,
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.ne twrote:
Soumen wrote:
On May 30, 10:33 am, Triple-DES <DenPlettf...@g mail.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_p tr,
boost::shared_a rray,
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...@com Acast.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
2884
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 to the method call and conversion would automagically take place, but this does not happen. I end up having to explicitly call auto_ptr.get(). Is there a good way around this? NR
20
3741
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
3499
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
1775
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 code that tried to have a container of auto_ptr (which I know is wrong, but thats not the point here) should *NOT* compile according to the standard. --- start original post --- There is a defined requirement, and if you're still in doubt, let...
10
2639
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
2922
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");
2
2392
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 : explicit auto_ptr(_Ty *_Ptr = 0) _THROW0() the second : auto_ptr(auto_ptr_ref<_Ty_Right) _THROW0()
2
1707
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 constructor below
17
10074
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
10356
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...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
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...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
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
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
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
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
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.