Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Experts,

I am little confusing over the following two situations.

Context1://Copy Constructor

class A
{
   public:
   int aVar;
   A(int tmp)
   {
     aVar=tmp;
   }
   A(A *ptr)//copy constructor part
   {
     aVar=ptr->aVar;
   }
};

int main()
{
  A a(10),b(&a);
}


Context2://Same result of copy constructor
class A
{
   public:
   int aVar;
   A(int temp)
   {
     aVar=temp;
   }
   A()
   {
   }
};

int main()
{
 A a(10),b;
 b=a;//just like a copy constructor
}

I think no need of explanation for the code is required. In the first case I am using a copy constructor. In the second case I am just assigning the second object(b) to first object(a). If this is the case, then why we required copy constructors? Please help me on this..

Thanks and Regards
Arun
Posted
Updated 16-Apr-11 10:45am
v3

In the second case, if you don't specify a copy constructor the compiler uses a default one which makes a shallow copy of the object.

Ps. a copy constructor is declared as a reference to a constant object of the same class
A(const A& a);


-Robotex
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Apr-11 15:59pm    
Correct, a 5.
--SA
Graham Shanks 16-Apr-11 16:55pm    
Not quite correct. In the second case, b has been created when it has been declared in the first line in main. The second line assigns a to b, using the assignment operator
Sergey Alexandrovich Kryukov 16-Apr-11 20:23pm    
Agree, I did not pay attention.
--SA
Actually in the second case, you are using the assignment operator. Again, just like the copy constructor, if you do not write an assignment operator then the compiler will generate one for you. The compiler generated assignment operator will be like the following:

ClassA& operator=(const ClassA& rhs)
{
  aVar = rhs.aVar;
  return *this;
}
 
Share this answer
 
Comments
Renshou 16-Apr-11 17:30pm    
Correct, I forgot it was assigned after.
Sergey Alexandrovich Kryukov 16-Apr-11 20:24pm    
My 5.
--SA
Arun India 17-Apr-11 0:27am    
So are you saying that both are same..?
Whever you write
class A
{
    int m;
};


The compiler will do
class A
{
    int m;
public:
    A() :m() {} 
    A(const A& a) :m(a.m) {}
    A& operator=(const A& a) { m=a.m; return *this; }
    ~A() {}
};


hence, unless you explicitly declare one or all of those function as private, things like
a1 = a2;
return a;
fn(a);
will always implicitly resolve to the compiler generated default.

There is people who think it would be better if such "autogenerated things" woludn't be there, but this way, an old C99 struct cannot copy.
 
Share this answer
 
Comments
Arun India 17-Apr-11 5:00am    
Thank you Emilio for giving me the exact answer, and for your valuable time..
Well look buddy!!! a copy constructor and assignment operator (via operator overloading) as well as default constructor and destructor are by default created for you whenever you define a class in C++.

Then why do we need to define them ourselves?

One and a simple answer to let it happened in our own way!!!
How we like these things to be done, just make it...

A default copy constructor and assignment operator are used to make shallow copy of the assigned object!!!
but you can override this ongoing default behavior by using your own defined/customized behavior which is needed in many situation.

For example:
<pre>class A
{
public:
int* a;
int b;
A()
{
b = 10;
a = &b;
}
}

int main()
{

A a1 = new A();
A a2(a1); //copy construction
delete a1;
cout << *(a2.b); //will produce an error
return 0;
}</pre>

because in shallow copy a2.a = a1.a = address of a1.b, now a1 is destroyed so address of a1.b is no longer valid.

This kind of problem can be omitted using your own copy constructor.
just add the following copy constructor to definition of A:
<pre>
A& A(const A& a1)
{
b = a1.b;
a = &b;
return &this;
}
</pre>
Hope it will help you !!! :)
 
Share this answer
 
v2
Comments
Arun India 18-Apr-11 0:52am    
Hi Bharat Kumar Arya
You gave the exact and the simple explanation and clarify my doubts. Thank you very much for your reply. My 5
Regards
Arun
Bharat Kumar Arya 18-Apr-11 6:03am    
Sorry copy constructor has a little mistake!!! :(
I am correcting it...
<pre>
A& A(const A& a1)
{
b = a1.b;
a = &b;
return *this;
}
</pre>

Well its not code but the idea you have to understand...

and i am glad you have understood that...:)
A copy constructor is needed in order to initialize this object with a copy of the input object. Event if your 'other constructor' (i.e. A(int tmp)) do basically the same, the compiler doesn't know that and won't use it.
For instance, without a copy constructor. you couldn't do the following:
A a1;
A a2(a1);


(nor even:
A a2=a1;

)

Please note: as other have already pointed out, the compiler gently provides a default copy constructor for you, but this is a technical artifact that has nothing to do with the above distinction.
 
Share this answer
 
Comments
Arun India 17-Apr-11 4:59am    
Thank you very much for your reply..

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