is this style ok ?

1
2
3
4
5
6
7
8
9
10
11
KD_MESSAGE::KD_MESSAGE( const KD_MESSAGE& copyMe )
{
	if( this != &copyMe )
	{
		msgColor = copyMe.msgColor;
		msgFont = copyMe.msgFont;
		msgSize = copyMe.msgSize;
	}

        return *this;
}


Just take everything from the target,this is indeed copy constructor.
if( this != &copyMe ) I use this to see if both point to the same object...is this : obsolete,useless or ok people ?
Last edited on
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 != &copyMe)
  {
    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
Yay ! Thank you !
Topic archived. No new replies allowed.