Click here to Skip to main content
13,045,540 members (68,021 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I thought I had got it right but am confused now. Say we have following code:
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 1-Apr-13 1:09am
Updated 1-Apr-13 1:24am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

Other than that, the copy constructor is called implicitly when transfering arguments by value or returning by value. Hence, a function
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
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.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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.
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
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 " 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100