dervived class assignment operator

Discussion in 'C++' started by Christopher, Feb 26, 2009.

  1. Christopher

    Christopher Guest

    My derived class assignment operator is not working. It never seems to
    get the assignment done. I suspect I am not allowed to implement it
    this way, but I am unsure why. Can someone explain what is happening
    here?

    ---------------------------------------------------------
    struct Base
    {
    Base & operator = (const Base & rhs);

    int m_x;
    };

    struct Derived : public Base
    {
    Derived & operator = (const Derived & rhs);
    };

    -----------------------------------------------------------
    Base & Base::eek:perator = (const Base & rhs)
    {
    m_x = rhs.m_x;
    }

    Derived & Derived::eek:perator = (const Derived & rhs)
    {
    static_cast<Base>(*this) = static_cast<Base>(rhs);

    return *this;
    }


    I did this, because, currently, derived does not have any extra member
    data when compared to base.
    When it does, I thought I could handle them one by one after the
    static cast / base assignment I did above.
    I must be wrong?
     
    Christopher, Feb 26, 2009
    #1
    1. Advertising

  2. Christopher

    Kai-Uwe Bux Guest

    Christopher wrote:

    >
    > My derived class assignment operator is not working. It never seems to
    > get the assignment done. I suspect I am not allowed to implement it
    > this way, but I am unsure why. Can someone explain what is happening
    > here?
    >
    > ---------------------------------------------------------
    > struct Base
    > {
    > Base & operator = (const Base & rhs);
    >
    > int m_x;
    > };
    >
    > struct Derived : public Base
    > {
    > Derived & operator = (const Derived & rhs);
    > };
    >
    > -----------------------------------------------------------
    > Base & Base::eek:perator = (const Base & rhs)
    > {
    > m_x = rhs.m_x;
    > }
    >
    > Derived & Derived::eek:perator = (const Derived & rhs)
    > {
    > static_cast<Base>(*this) = static_cast<Base>(rhs);


    On the left hand side, you create a temporary Base object. To that
    temporary, something is assigned. This compiles, because the assignment
    operator is a member function.

    The following should work, but looks awful:

    static_cast<Base&>( *this ) = rhs;

    > return *this;
    > }
    >
    >
    > I did this, because, currently, derived does not have any extra member
    > data when compared to base.
    > When it does, I thought I could handle them one by one after the
    > static cast / base assignment I did above.
    > I must be wrong?


    No. But you might want to avoid casts:

    Derived & Derived::eek:perator = (const Derived & rhs) {
    Base::eek:perator= ( rhs );
    // other bases
    // more members
    return ( *this );
    }

    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 26, 2009
    #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. N4M
    Replies:
    17
    Views:
    629
    Daniel T.
    Aug 19, 2004
  2. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    509
    Barry Schwarz
    Feb 8, 2005
  3. Mike - EMAIL IGNORED

    base class copy ctor and assignment operator

    Mike - EMAIL IGNORED, Mar 21, 2006, in forum: C++
    Replies:
    1
    Views:
    340
    Alf P. Steinbach
    Mar 21, 2006
  4. Chris
    Replies:
    34
    Views:
    1,606
  5. Hicham Mouline
    Replies:
    2
    Views:
    736
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page