Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, I thought I had got it right but am confused now. Say we have following code:
C++
MyClass object1;
MyClass object2 = object1; // I thought here Copy Constructor is called instead of = operator right??

// But here, if I have:
MyClass object3;
object3 = object1; // In this case = operator would be called right??


I am a bit confused because in my code I have mostly everywhere only MyClass object2 = object1; type of calls and still my assignment (=) operator gets called.

Any help? Thank you.
Posted
Updated 1-Apr-13 1:24am
v2

If you want the copy constructor to be called, use
C++
MyClass object2 (object1);

Other than that, the copy constructor is called implicitly when transfering arguments by value or returning by value. Hence, a function
C++
MyClass myFunction (MyClass obj)
{
    ...
    return x;
}

would implicitly call the copy constructor for argument obj and for returning value x. Notice, however, that by modifying the function to
C++
MyClass& myFunction (MyClass& obj)
{
    ...
    return x;
}

the copy constructor will not be called. In these cases a reference is passed, which means internally that just a pointer is being transferred in and out.
 
Share this answer
 
Comments
Dan page 1-Apr-13 8:04am    
Hi, thanks for your reply. What gets called in such a case? MyClass object2 = object1; I guess the copy constructor also?
nv3 1-Apr-13 8:30am    
In that case the object is first created by the default constructor and then the assignment operator is called.
Dan page 1-Apr-13 8:32am    
No, I think that's not true :)
nv3 1-Apr-13 9:58am    
You are right. If the assignment happens in an initialization part of a definition, the copy constructor will be called.
Hi,

Your copy constructor will be called, if the object does not exist. Otherwise your assignment operator is used to copy the values from one object to another already existing object.

In your question in both cases your assignment operator will be invoked because your object already exist.
 
Share this answer
 
Comments
Dan page 1-Apr-13 8:24am    
Hi, no not true in the first case object 2 does not exist so copy constructor should called.
Dan page 1-Apr-13 10:04am    
no :) check last comment of nv3 also.
ps. THis line: MyClass object2 = object1; will call the Copy Constructor.
ramrooney 1-Apr-13 13:37pm    
Yup. Dan page - you are correct, in the first case the object is not present, so the copy constructor will be invoked. Hope you are clear now.
SoMad 1-Apr-13 15:37pm    
Sorry, you are right, I was wrong. Comment retracted.

Soren Madsen
ps. I've checked it seems I had leaked somewhere usage of a normal = operator
that is why the program was calling my = operator.
Otherwise, what I mentioned in my initial reply seem to be true.
 
Share this answer
 
Comments
SoMad 1-Apr-13 8:31am    
Now I am confused.

Soren Madsen
Dan page 1-Apr-13 8:33am    
Hi, meaning all the assumptions I had made in my comments (in the "code" section) in my initial post seem to be true.
SoMad 1-Apr-13 8:49am    
I see. You wrote "...in my initial reply..." so I thought you were referring to the first reply you made to Solution 1.

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900