Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 2:09am
Edited 1-Apr-13 2:24am
v2
Rate this: bad
good
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.
  Permalink  
Comments
Dan page at 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 at 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 at 1-Apr-13 8:32am
   
No, I think that's not true :)
nv3 at 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
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Dan page at 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 at 1-Apr-13 10:04am
   
no :) check last comment of nv3 also.
ps. THis line: MyClass object2 = object1; will call the Copy Constructor.
ramrooney at 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 at 1-Apr-13 15:37pm
   
Sorry, you are right, I was wrong. Comment retracted.
 
Soren Madsen
Rate this: bad
good
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.
  Permalink  
Comments
SoMad at 1-Apr-13 8:31am
   
Now I am confused.
 
Soren Madsen
Dan page at 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 at 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)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 175
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2014
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