Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If both copy constructor and assignment operator are doing same task i.e. (member wise copy) then why we are using it separately.

C++
class A
{

};
int main()
{
A a2;
A a2=a1;  //copy const.
A a3;
a3=a2;   //assignment opeartor

}


i think both will do same job then why we are not using only one concept either copy const or assignment operator


Please help me if there is concept behind this

Thanks in advance

[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 8-Jul-14 22:53pm
v2
Comments
George Jonsson 9-Jul-14 4:17am    
Using capital letters is considered loud and rude.
OriginalGriff 9-Jul-14 4:53am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

There is an important difference between a copy constructor and the assignment operator. A copy constructor always starts out with an un-initialized object, while the assignment operator starts with a valid object.

This difference becomes important when an object has allocated resources, for example a chunk of memory. A little example will make that point clear. Imagine your class A contains a pointer to some allocated memory:
C++
class A
{
    char* m_string;
};

In the constructors you will have to initialize this member, for example
C++
class A
{
public:
    A (const A& other)
    {
        m_string = new char [other.m_string.strlen() + 1];
        strcpy (m_string, other.m_string);
        // yes, there are better ways to do that!
    }
    char* m_string;
};

Now consider an assignment operator. It first has to de-allocate any memory that might have been allocated and then do the assignment.
 
Share this answer
 
Comments
Espen Harlinn 10-Jul-14 12:04pm    
5'ed :-)
nv3 (solution 1) and Prakash257 (solution 2) are both correct, in that the code for the two operators may need to be different, and that the operators are called under different
circumstances.

I'd like to add that, if you write your own copy constructor and assignment operator, you can avoid duplicate code by calling the assignment operator from the copy constructor, e. g.:
C++
class A{
   int num;
   char *name;
public:
   A() :
      num(0),
      name(nullptr)  // never forget to intialize that pointer!
   {
   }
   A(const A& a) :
      name(nullptr)  // never forget to intialize that pointer!
   {
      operator=(a);
   }
   ~A()
   {
      delete [] name; // beware: this would fail with an uninitialized pointer!
   }
   A& operator=(const A& a)
   {
      delete [] name; // beware: this would fail with an uninitialized pointer!
      name = nullptr;
      if (a.name != nullptr) {
         name = new char[strlen(a.name)+1];
         strcpy(name, a.name);
      }
      num = a.num;
      return *this;
   }
   int get const { return num; }
   int set(int n) { num = n; }
   ...
};

Of course, in this example you don't really save much. But it might still be handy when you have 10 or more data members to copy.
 
Share this answer
 
C++
#include "iostream"
#include "stdio.h"
using namespace std; 
class CopyConstructor
{
  public:
    CopyConstructor () { } ;
    CopyConstructor(const CopyConstructor &t)
     {
       cout<<"Copy constructor called "<<endl;
     }
    CopyConstructor& operator = (const CopyConstructor &t)
    {
      cout<<"Assignment operator called "<<endl;   
    }
};
int main()
{
  CopyConstructor obj1, obj2;
  obj2 = obj1;
  CopyConstructor obj3 = obj1;
  getchar();
  return 0;
}

Output:
Assignment operator called
Copy constructor called

Explanation:
Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. And assignment operator is called when an already initialized object is assigned a new value from another existing object.
obj2 = obj1; // calls assignment operator, same as "obj2.operator=(obj1);"
CopyConstructor obj3 = obj1; // calls copy constructor, same as "CopyConstructor obj3(obj1);"
 
Share this answer
 
v2

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