Default operator=() for a derived class?

Discussion in 'C++' started by jl_post@hotmail.com, Mar 4, 2011.

  1. Guest

    Dear C++ community,

    You know how C++ classes will automatically provide a default
    operator=() method that calls each of its member variables'
    operator=() ?

    Well, I was wondering: Does the default operator=() method of a
    derived class call operator=() on each of the base class' member
    variables? Or does it just call the base class' opeator=() for those,
    while still using operator=() on its remaining member variables?

    Here's an example program to illustrate:

    (Note that the Base class prepends "copy of" when copying member
    variables, while the Derived class does not have an operator=() method
    explicitly defined.)

    === START OF CODE ===

    #include <iostream>
    #include <string>

    class Base
    {
    public:
    std::string a;
    std::string b;

    Base& operator=(const Base & that)
    {
    a = "copy of " + that.a;
    b = "copy of " + that.b;
    return *this;
    }

    virtual std::string toString()
    {
    return "a='" + a + "'; b='" + b + "'";
    }
    };

    class Derived : public Base
    {
    public:
    std::string c;

    virtual std::string toString()
    {
    return Base::toString() + "; c = '" + c + "'";
    }
    };


    int main(int argc, char ** argv)
    {
    Derived var1; var1.a = "a"; var1.b = "b"; var1.c = "c";
    Derived var2; var2.a = "x"; var2.b = "x"; var2.c = "x";

    var2 = var1;

    std::cout << var2.toString() << std::endl;

    return 0;
    }

    === END OF CODE ===

    What should this program print? When I run it, I see this:

    a='copy of a'; b='copy of b'; c = 'c'

    which implies that the default Derived::eek:perator=() method called
    Base::eek:perator=() and also invoked std::string::eek:perator=() on
    variable c. If Base::eek:perator=() wasn't used, then we wouldn't see
    "copy of" in front of a and b's values.

    This makes sense (to me) as default behavior for the operator=()
    method of a derived class. My main question is: Is this behavior
    explicitly stated in the C++ specifications? (I want to avoid
    undefined behavior.)

    (I tried searching for this, and while I found similar examples, I
    was not able to verify if this is explicitly correct behavior.)

    Thanks.

    -- Jean-Luc
    , Mar 4, 2011
    #1
    1. Advertising

  2. Guest

    > wrote in news:292997f1-fd3f-4bec-a5d7-088444497674
    > @a21g2000prj.googlegroups.com:
    >
    > >    Well, I was wondering:  Does the default operator=() method of a
    > > derived class call operator=() on each of the base class' member
    > > variables?  Or does it just call the base class' opeator=() for those,
    > > while still using operator=() on its remaining member variables?



    On Mar 4, 3:20 pm, Paavo Helde <> replied:
    > Yes, the latter. This is specified in 12.8/13:
    >
    > "The implicitly-defined copy assignment operator for class X performs
    > memberwise assignment of its subobjects. The direct base classes of X are
    > assigned first, in the order of their declaration in the base-
    > specifierlist, and then the immediate nonstatic data members of X are
    > assigned, in the order in which they were declared in the class
    > definition."



    Thanks, Paavo. That's exactly the kind of answer I was looking
    for.

    -- Jean-Luc
    , Mar 7, 2011
    #2
    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. Alicia
    Replies:
    3
    Views:
    944
    jjr2004a
    Nov 24, 2004
  2. Replies:
    4
    Views:
    406
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    394
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
  5. David
    Replies:
    3
    Views:
    401
    Grizlyk
    Jan 29, 2008
Loading...

Share This Page