copy constructors and overloading = What is the difference?

Discussion in 'C++' started by pauldepstein@att.net, Dec 7, 2005.

  1. Guest

    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
    , Dec 7, 2005
    #1
    1. Advertising

  2. Re: copy constructors and overloading = What is the difference?

    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
    Irina Marudina, Dec 7, 2005
    #2
    1. Advertising

  3. Marcus Kwok Guest

    Re: copy constructors and overloading = What is the difference?

    Irina Marudina <> wrote:
    > 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);


    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=.

    --
    Marcus Kwok
    Marcus Kwok, Dec 7, 2005
    #3
  4. Re: copy constructors and overloading = What is the difference?

    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.
    Ravi Srivastava, Dec 7, 2005
    #4
  5. Re: copy constructors and overloading = What is the difference?

    >> 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.


    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 --------------

    >> 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.


    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.

    >> 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.


    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.

    >> I hope this may clarify your doubt.

    Me too.

    Regards,
    Irina Marudina
    Irina Marudina, Dec 7, 2005
    #5
  6. red floyd Guest

    Re: copy constructors and overloading = What is the difference?

    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
    red floyd, Dec 7, 2005
    #6
  7. Ben Pope Guest

    wrote:
    > 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
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Dec 9, 2005
    #7
  8. Earl Purple Guest

    Re: copy constructors and overloading = What is the difference?

    Irina Marudina wrote:
    > >> 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.

    >
    > 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.
    Earl Purple, Dec 9, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,279
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    588
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    596
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    394
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    465
Loading...

Share This Page