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! ]