copy constructors and overloading = What is the difference?

P

pauldepstein

A book I have describes a class called vector.

It then explains (and I understand) the concept of a copy constructor,
which is needed to interpret statements like

vector b = a;


But then I get confused when it speaks of overloading =, and explains
that this concept is needed to interpret statements like b = a;
when b and a are vectors.


But didn't we take care of the b = a problem when we set up the
copy constructor?

Why on earth do we need to do the same thing twice (as it appears to
me)?

Apparently, we set up the copy constructor to allow copies of objects
in a class. But, for some unfathomable (to me) reason, this isn't
quite good enough and we need to overload the = operator (again in
order to allow copies of objects in a class.)

Could someone explain the difference between these two concepts, so
that I can see why both the copy constructor and the overloading are
needed?

Thank you,

Paul Epstein
 
I

Irina Marudina

The copy constructor is most of all a constructor. This means that it
is called only if no instance is yet created. The code you gave:
vector b = a;

is equivalent to :

vector b(a);

As you can see this is another constructor that takes as parameter a
reference to an object of the same type.

The assignment operator on the other hand is needed when two instances
are already created but you want (at some point of the program
execution) the one to have the same value as the other. Example:

vector a;
vector b;
//....
a = b;

If you want to create a copy of an object it is not efficient to create
it with the default constructor and after that to call the overloaded
operator= . It is better to create a copy with the copy constructor.
But if you want to change a value of an object later in the program -
you cannot do this with the copy constructor because the object is
already constructed. So in this case you need the operator=;

Regards,
Irina Marudina
 
M

Marcus Kwok

Irina Marudina said:
The copy constructor is most of all a constructor. This means that it
is called only if no instance is yet created. The code you gave:


is equivalent to :

vector b(a);

The similarity in syntax between initialization in

vector b = a;

and assignment in

b = a;

is most likely what is causing the confusion. So, as Irina said,

vector b = a;

is really a copy constructor call, which is different from operator=.
 
R

Ravi Srivastava

Copy constructor requires overloading of = operator in order to provide
a=b kind of interface, otherwise a=b will end up copying the address of
b to a not the deep copy of object.
If you do not overload = operator then a=b will in turn point to the
same object in memory. Which will end up creating inconsistency.
However if you overload = operator then a=b will create two different
copies of the object each pointed to by a and b. In that way you will
get an object 'a' which is exact copy of 'b' but 'b' will remain immune
to any modification made in 'a' and vice versa.

I hope this may clarify your doubt.
 
I

Irina Marudina

Copy constructor requires overloading of = operator in order to provide
Copy constructor does NOT require operator=! The "A a=b;" so called
"interface" is just a syntax sugar for
"A a(b);". If you don't believe me, try this:

-------------- Example start --------------
#include <iostream>
using std::cout;
using std::endl;

class A
{
private:
int m_i;
A& operator=(const A&); // forbid the compiler to generate operator=
for you

public:
A(int i = 0)
{
m_i = i;
}

A(const A& a)
{
m_i = a.m_i;
}

int get()
{
return m_i;
}
};

int main()
{
A a1(5);
A a2 = a1;

cout << a1.get() << ' ' << a2.get() << endl;

cout << &a1 << ' ' << &a2 << endl;

//a2 = a1; // Does not compile because there is no public operator=

return 0;
}
-------------- Example end --------------

Check the addresses of the two objects and see for yourself if they are
the same when no operator= is present.
To be sure that the operator= is actually not present uncomment the
line "a2 = a1" and compile.

Operator= NEVER creates an object! It is just an operator (as its name
states), not a constructor. So it just assigns new values to the
members of an already created one.
Me too.

Regards,
Irina Marudina
 
R

red floyd

From the holy Standard:

8.5/11: The form of initialization (using parenthesis or =) is generally
insignificant, but does matter when the entity being initialized has a
class type; see below. ...

8.5/12 The initialization .... is called /copy-initialization/ and is
equivalent to the form
T x = a;
The initializtion ... is called /direct-initialization/ and is
equivalent to the form
T x(a);

8.5/14 ... If the destination type is a (possibly cv-qualified) class type:
.... If the initiialization is direct-initialization, or if it is
copy-initialization, where the cv-unqualified version of the source type
is the same class as, or a derived class of, the class of the
destination, constructors are considered.

----

In other words

class A {
//....
};

A a;
A b = a; // calls copy constructor
 
B

Ben Pope

A book I have describes a class called vector.

It then explains (and I understand) the concept of a copy constructor,
which is needed to interpret statements like

vector b = a;


But then I get confused when it speaks of overloading =, and explains
that this concept is needed to interpret statements like b = a;
when b and a are vectors.


But didn't we take care of the b = a problem when we set up the
copy constructor?

Why on earth do we need to do the same thing twice (as it appears to
me)?

When you see an declaration and initialisation like:
vector a;
vector b = a;

Then it's not an assignment, it's a construction:
vector b(a);

The syntax is just there for convenience, the following statements are
all initialisation:

// These two are semantically identical, the first form
// is "converted to" the second form
int a = 3;
int a(3);

// And again here
int b = a;
int b(a);

Hope that makes sense.

Ben Pope
 
E

Earl Purple

Irina said:
Copy constructor does NOT require operator=() The "A a=b;" so called
"interface" is just a syntax sugar for
"A a(b);". If you don't believe me, try this:

Further to your example, a class that has a const member or a reference
member can (often) be copied but cannot be assigned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top