Link to home
Start Free TrialLog in
Avatar of chikucoder
chikucoder

asked on

copy constructor

under what all senario's copy constructor is invoked.hurry up guys
Avatar of shivsa
shivsa
Flag of United States of America image

Copy constructor is invoked when you have to assign an instance of the class to another instance of the class. For example in your case,  a sample of the code would be :

SomeClass A, B;

A.Assign(); // Assuming you have a method Assign which fills this.
B = A;        // Now the copy constructor is invoked
Purpose of copy constructor is to make a new object based on an existing object, where both objects belong to the same class.

another example is
SomeClass B( A ) ;
Avatar of chikucoder
chikucoder

ASKER

how it is different from assignment operator
ASKER CERTIFIED SOLUTION
Avatar of shivsa
shivsa
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1. At the time of object creation
2.At datatype casting time
3. At = assignment operator invokation
1) MyObject a(myObjectInst);
2) MyObject a = myObjectInst;
3) a = (MyObject)myObjectInst;       // if a has been initialised it will call the copy ctor then the assignment op...
                                                    // if not then it will call the copy ctor twice,
                                                         // once for the cast and once for the initialisation of a.