Hey all,
So, if I create a class, and give it a constructor, like so:
class Thing
{
public:
Thing( int );
};
Then I can do implicit conversions with that class, like so
int main()
{
int i = 10;
Thing t = i;
}
However, I ran into a case where I have the following
class Skeleton
{
public:
Skeleton() : _root( NULL ){}
Skeleton( const Skeleton& skeleton ) : _root( new Joint
( *skeleton._root ) ){}
~Skeleton(){ if( _root( NULL ) ) delete root; }
Joint* _root;
};
int main()
{
Skeleton skel1;
Skeleton skel2;
skel2 = skel1;
std::cout << skel1._root << std::endl;
std::cout << skel2._root << std::endl;
}
In doing this, my copy constructor isn't actually getting called. What
does happen is that skel2.root now has the same address as skel1.root,
so I get problems later on when the two objects fall out of scope and
the destructor does delete on the same address twice.
I instead have to create a separate operator= for the class. Am I
missing something, or why is the explicit assignment operator
necessary here?
{ Short answer: you need an assignment operator. The Law of The Big
Three states "A class with any of {destructor, assignment operator,
copy constructor} generally needs all 3"; see FAQ 27.10. -mod/sk }
Thanks.
Dave
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]