Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

My question is on assignment operator on class objects.Suppose that I have a class Calculator as below.

C++
Class Calculator
{
 public:

  // BODY OF THE CLASS

};

// Trying to create class object using parameterized constructor

Calculator obj1( argument1, argument2);

// Case1

Calculator obj2 = obj1; 


// Case2

calculator obj3;  

obj3 = obj1;



Please see above Case1 and Case2, I have below questions

a) What is basic difference between above two cases? I mean how does this two way of assignment differ programatically?

b) Which case would call copy constructor. ( assume that copy constructor is defined in class)?

c) When should somebody use case1 and when case2?
Posted
Updated 23-Oct-13 17:15pm
v2

  • Case 1 is initialization, that is the copy ctor is called.
  • Case 2 is assignment and hence assignment operator is called.


Try the following code:
C++
#include <iostream>
class A
{
  int a;
  public:
  A(int a=0):a(a){}
  A(const A & other){
    a = other.a;
    std::cout << "copy ctr" << std::endl;
  }
  A & operator=(const A& other) {
    a=other.a;
    std::cout << "= operator" << std::endl;
    return *this;
  }
};

int main()
{
  A a1(5);
  A a2 = a1;
  A a3;
  a3 = a1;
}
 
Share this answer
 
Comments
joyjhonson 23-Oct-13 9:05am    
Thank you so much..sort and simple explanation
CPallini 23-Oct-13 9:11am    
You are welcome.
hi,
Generally,
There are three general cases where the copy constructor is called instead of the assignment operator:

1).When instantiating one object and initializing it with values from another object .
2).When passing an object by value.
3).When an object is returned from a function by value.

The assignment operator is used to copy the values from one object to another already existing object.
C++
MyClass myObject1(someValueHere); // calls MyClass constructor
MyClass myObject2; // calls MyClass default constructor
myObject2= myObject1; // calls MyClass assignment operator


In this case, myObject2 has already been created by the time the assignment is executed. Consequently, the MyClass assignment operator is called. The assignment operator needs to be overloaded as a member function.

A copy constructor is called if the object being copied into does not already exist.
C++
MyClass myObject1(someValueHere); // calls Cents constructor
MyClass myObject2= myObject1; // calls Cents copy constructor!



Because the second statement uses an equals symbol in it, you might expect that it calls the assignment operator. However, it doesn’t! It actually calls a special type of constructor called a copy constructor. A copy constructor is a special constructor that initializes a new object from an existing object.

The purpose of the copy constructor and the assignment operator are almost equivalent — both copy one object to another. However, the assignment operator copies to existing objects, and the copy constructor copies to newly created objects.

do google for more
hope this helps!
 
Share this answer
 
Comments
joyjhonson 23-Oct-13 9:13am    
Thank you for beautiful explanation.
chandanadhikari 23-Oct-13 9:20am    
please rate the answer if you liked it (you need to click on the stars in the solution heading) -thanks
For a) and b), it is already answered in other solutions...

For c), you should generally prefer Case 1 as it is a good pratice to initialize an object when it is declared. But you should consider using the follwing form instead:
C++
Calculator obj2(obj1);


It is clear that you want to use the copy constructor and as far as I know event though the compiler optimize away the copy in case 1, you still need a default constructor and assignment operator for code to compile. Same with case 2. Thus the above syntax might works in some case where the other syntax is not allowed (for example if default constructor is not defined).


As a side note for parameter-less constructor, you effectively want to write it without empty ().
C++
Calculator fn(); // Declare a function fn that return a Calculator by value.
Calculator obj; // Create a calculator object


The reason is essentially historical since function prototype were created before classes... and changing that (by adding a required function keyword for déclarations for example) would have break existing code.
 
Share this answer
 
v2
For a) and b), it is already answered in other solutions...

For c), you should generally prefer Case 1 as it is a good pratice to initialize an object when it is declared. But you should consider using the follwoing form instead:
C++
Calculator obj2(obj1);


It is clear that you want to use the copy constructor and as far as I know event though the compiler optimize away the copy in case 1, you still need a default constructor and assignment operator for code to compile. Same with case 2. Thus the above syntax might works in some case where the other syntax is not allowed (for example if default constructor is not defined).


As a side note for parameter-less constructor, you effectively want to write it without empty ().
C++
Calculator fn(); // Declare a function fn that return a Calculator by value.
Calculator obj; // Create a calculator object


The reason is essentially historical since function prototype were created before classes... and changing would have break existing code.
 
Share this answer
 

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