Saturday, September 24, 2011

Constructor , Copy Constructor and Synthesized Default Constructor

Copy Constructor :
Get invoked when :
1 Object is created and initialized at same time .
2.When function returns object .
3.A Value parameter is initialized from its corresponding argument in function call.
in pass by value .

cc is called in this cases
student S2(S1)
student S2 =S1

copy constructor will not invoked in these cases :
S2 = S1 here bitwise copy will happen Assignment operator must be overloaded for deep
copying scenario.
Student S1 ;


When is a Constructor Synthesized ?
1.Class has member object of another class which has default Constructor. [to invoke member objs ctor]
2.Base class with default Ctor [to invoke the default ctor of each of its immediate base classes in order of their declarations]
3.If  class or any of its base class has virtual function. [to initialize VPTR -->VTBL]
4. Class with virtual Base Class.[to initialize pointer to virtual base class]

No comments:

Post a Comment

Followers