Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > User defined co...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 9479 of 10096
Post > Topic >>

User defined copy constructors

by Sebastian Karlsson <Sebastian.Karlsson@[EMAIL PROTECTED] > Apr 9, 2008 at 01:43 PM

Hi, I need to create copy constructors for a lot of my types in order
to fix the common pointer problems of shallow copying. I've never
thought much about it before but I've checked out the most common
tutorials and they seem to suggest what I've used in the past, namely:

class X
{
public:
	X( const X& a_Other ) { member = a_Other.member; }
	Y member;
};

The problem here is that first Ys default constructor is invoked and
then assignment. Using the default generated one only Ys copy
constructor is invoked. Considering how the code looks this is of
course pretty obvious. So I tried to use an initialization list in the
copy constructor instead, which gave me the same behaviour as the
default copy constructor. I can't recall seeing this used elsewhere
though which kind of boggles me.

The real question though is: Is it possible to first create a shallow
copy using the default copy constructor and then afterwards clean up
the pointer problems etc. I would stay have to pay for some unecessary
pointer copying, but it would save me from maintaining huge
initilization lists for types which mostly have default copyable
members.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 4 Posts in Topic:
User defined copy constructors
Sebastian Karlsson <Se  2008-04-09 13:43:23 
Re: User defined copy constructors
Ulrich Eckhardt <dooms  2008-04-09 17:56:48 
Re: User defined copy constructors
Sebastian Karlsson <Se  2008-04-10 19:40:51 
Re: User defined copy constructors
Ulrich Eckhardt <dooms  2008-04-11 11:33:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:26:39 CDT 2008.