is this style ok ?
Feb 12, 2012 at 1:51am UTC
1 2 3 4 5 6 7 8 9 10 11
KD_MESSAGE::KD_MESSAGE( const KD_MESSAGE& copyMe )
{
if ( this != ©Me )
{
msgColor = copyMe.msgColor;
msgFont = copyMe.msgFont;
msgSize = copyMe.msgSize;
}
return *this ;
}
Just take everything from the target,this is indeed copy constructor.
if ( this != ©Me )
I use this to see if both point to the same object...is this : obsolete,useless or ok people ?
Last edited on Feb 12, 2012 at 2:20am UTC
Feb 12, 2012 at 2:47am UTC
This is not the usual way implementing a copy constructor. This would be better:
1 2 3 4 5
KD_MESSAGE::KD_MESSAGE(const KD_MESSAGE& copyMe)
: msgColor(copyMe.msgColor),
msgFont(copyMe.msgFont),
msgSize(copyMe.msgSize)
{}
The body of your code is appropriate for assignment operator overloading:
1 2 3 4 5 6 7 8 9 10
KD_MESSAGE& operator =(const KD_MESSAGE& copyMe)
{
if (this != ©Me)
{
msgColor = copyMe.msgColor;
msgFont = copyMe.msgFont;
msgSize = copyMe.msgSize;
}
return *this ;
}
To avoid self-assignment is just fine and indeed its only possible in the second case, because in a copy construction
copyMe can never be the object that you are going to construct, right ;-)
Last edited on Feb 12, 2012 at 2:53am UTC
Feb 12, 2012 at 3:17am UTC
Yay ! Thank you !
Topic archived. No new replies allowed.