copy constructor

under what all senario's copy constructor is invoked.hurry up guys
Who is Participating?
shivsaConnect With a Mentor Commented:
in above example
B = A;  // invoke the assignment operator after invoking the default constructor.


SomeClass B( A ) ;  // invoke the copy constructor
queueClass y = x ;  // invoke the copy too.
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 ) ;
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

chikucoderAuthor Commented:
how it is different from assignment operator
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.