Don't do it if the member is a pointer to a dynamically allocated block, eg:
class C3{
char * Msg;
public:
C3(const char * aMsg){Msg = strdup(aMsg);} //strdup makes a copy of aMsg in the heap.
/* Don't do it this way
* C3(const char * aMsg) : Msg(aMsg){}
* It's problematic because Msg becomes dependant on aMsg, if aMsg is deleted
* Msg will point to a invalid block, access of it result in AV
**************************************************************************/
};
class C1{
int x, y;
public:
C1(int x, int y) : x(x), y(y){}
/* here x, y are basic data type(int), you would otherwise rewrite it as:
* C1(int x, int y)
* {
* this->x = x;
* this->y = y;
* }
* I think the former is smarter, how do you like it?
************************************************************************/
C1( const C1 aC1) // a copy constructor is defined
{
x = aC1->x ; y = aC1 -> y;
}
};
class C2{
C1 MyC1;
public:
C2(const C2& aC2) : MyC1(aC2.MyC1) {}
/* since C1 has an copy constructor, so its perfectly legal to construct
* MyC1 this way.(Compiler will provide a copy constructor for you is none
* is defined)
* Your may otherwise rewrite the above as:
* C2(const C2& aC2)
* {
* MyC1 = aC2.MyC1;
* }
* **********************************************************************/
};