Re: Copy assignment for derived class when base class has privatemembers

Discussion in 'C++' started by Karl Heinz Buchegger, Aug 6, 2003.

  1. Jochen Zeischka wrote:
    >
    > I have the impression that I'm doing something simple in a complex way...


    Right.

    Rule #1: Make sure you put code in the class where it belongs!

    It is *not* the business of the Derived class to take care of
    the exact details of the Base class: what needs to be assigned
    and how to do it. It is the business of the Base class to take
    care of that. All you need to do in the Derived operator=, is to
    call the Base::eek:perator= to give it a chance to do it's work.

    Derived& Derived::eek:perator=(const Derived& d)
    {
    Base::eek:perator=( d ); // let the base class operator=
    // do it's work

    // and now handle the Derived member variables.

    return *this;
    }

    Note: Since in this specific example, Derived has no work to
    do on it's own, you wouldn't need the whole Derived::eek:perator=
    at all. The compiler generated one does exactly the same thing.
    But I understand that this is just a simplified example for
    posting purposes.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 6, 2003
    #1
    1. Advertising

  2. Re: Copy assignment for derived class when base class has private members

    Thanks a lot!

    I was quite sure there had to be a simpler way, but I never would have
    thought about using the copy assignment as "Base::eek:perator=(d);"

    I was thinking of something like "*this = Base(d);" to call Base::eek:perator=,
    but that is ofcourse non-sense, as the return type doesn't match *this.

    Thanks again!

    Jochen



    "Karl Heinz Buchegger" <> wrote in message
    news:...
    >
    >
    > Jochen Zeischka wrote:
    > >
    > > I have the impression that I'm doing something simple in a complex

    way...
    >
    > Right.
    >
    > Rule #1: Make sure you put code in the class where it belongs!
    >
    > It is *not* the business of the Derived class to take care of
    > the exact details of the Base class: what needs to be assigned
    > and how to do it. It is the business of the Base class to take
    > care of that. All you need to do in the Derived operator=, is to
    > call the Base::eek:perator= to give it a chance to do it's work.
    >
    > Derived& Derived::eek:perator=(const Derived& d)
    > {
    > Base::eek:perator=( d ); // let the base class operator=
    > // do it's work
    >
    > // and now handle the Derived member variables.
    >
    > return *this;
    > }
    >
    > Note: Since in this specific example, Derived has no work to
    > do on it's own, you wouldn't need the whole Derived::eek:perator=
    > at all. The compiler generated one does exactly the same thing.
    > But I understand that this is just a simplified example for
    > posting purposes.
    >
    > --
    > Karl Heinz Buchegger
    >
     
    Jochen Zeischka, Aug 6, 2003
    #2
    1. Advertising

  3. Jochen Zeischka wrote:
    >
    > Thanks a lot!
    >
    > I was quite sure there had to be a simpler way, but I never would have
    > thought about using the copy assignment as "Base::eek:perator=(d);"


    An operator is just another function with a fancy name :)

    >
    > I was thinking of something like "*this = Base(d);" to call Base::eek:perator=,
    > but that is ofcourse non-sense, as the return type doesn't match *this.


    That's not the reason. The problem is that *this is still a Derived object
    and thus the operator= of Derived would be called -> endless recursion.
    But casting 'this' to a Base* would have worked.

    *((Base*)this) = d;


    Nevertheless: calling the operator in the shown way is IMHO simpler and
    it avoids a cast, which is always a good thing.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 6, 2003
    #3
  4. Jochen Zeischka wrote:
    >
    > The "Base::eek:perator=(d);" solution works perfectly fine and is absolutely
    > what I'm looking for. Just for fun though I tried the type-cast version
    > "*((Base*)this) = d;" and it compiles but it gives incorrect results.
    >
    > Just to inform you ;-)


    Hmm. This works for me:

    #include <iostream>
    using namespace std;

    class Base
    {
    public:
    Base( int i ) : m_Test( i ) {}

    Base& operator=( const Base& Arg ) { m_Test = Arg.m_Test; return *this; }

    int m_Test;
    };

    class Derived : public Base
    {
    public:
    Derived( int i = 0 ) : Base( i ) {}
    Derived& operator=( const Derived& Arg ) { *(Base*)this = Arg; return *this; }
    };

    int main()
    {
    Derived a, b( 4 );

    cout << a.m_Test << endl; // expected 0
    a = b;
    cout << a.m_Test << endl; // expected 4

    return 0;
    }

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 6, 2003
    #4
    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.

Share This Page