virtual assignment operator/polymorphism question

Discussion in 'C++' started by Stephan Kurpjuweit, Jun 14, 2004.

  1. Hi,

    could you please help me with the following example?


    struct Base
    {
    virtual Base& operator = (const Base &k) {}
    };

    struct Derived: public Base
    {
    // 1
    virtual Derived& operator = (const Derived &k) {}

    // 2
    virtual Base& operator = (const Base &k) {}
    };

    int main(int argc, char* argv[])
    {
    Derived *dp = new Derived();
    Base *bp = new Derived(*dp);

    *bp = *dp;

    return 0;
    }


    Why does the assignment in main lead to a call to the second assignment
    operator (// 2) and not to the first one (//1)?

    Thank you,
    Stephan
     
    Stephan Kurpjuweit, Jun 14, 2004
    #1
    1. Advertising

  2. Stephan Kurpjuweit wrote:
    > Hi,
    >
    > could you please help me with the following example?
    >
    >
    > struct Base
    > {
    > virtual Base& operator = (const Base &k) {}
    > };
    >
    > struct Derived: public Base
    > {
    > // 1
    > virtual Derived& operator = (const Derived &k) {}
    >
    > // 2
    > virtual Base& operator = (const Base &k) {}
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > Derived *dp = new Derived();
    > Base *bp = new Derived(*dp);
    >
    > *bp = *dp;
    >
    > return 0;
    > }
    >
    >
    > Why does the assignment in main lead to a call to the second assignment
    > operator (// 2) and not to the first one (//1)?


    Because // 1 does not override the assignment operator in Base, but the
    // 2 does.

    V
     
    Victor Bazarov, Jun 14, 2004
    #2
    1. Advertising

  3. "Stephan Kurpjuweit" <> wrote in message
    news:cakf2p$6pr$...
    > Hi,
    >
    > could you please help me with the following example?
    >
    >
    > struct Base
    > {
    > virtual Base& operator = (const Base &k) {}
    > };
    >
    > struct Derived: public Base
    > {
    > // 1
    > virtual Derived& operator = (const Derived &k) {}
    >
    > // 2
    > virtual Base& operator = (const Base &k) {}
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > Derived *dp = new Derived();
    > Base *bp = new Derived(*dp);
    >
    > *bp = *dp;
    >
    > return 0;
    > }
    >
    >
    > Why does the assignment in main lead to a call to the second assignment
    > operator (// 2) and not to the first one (//1)?
    >


    1) *bp is of static type Base, so the compiler generates a virtual call to
    Base::eek:perator=(const Base&).

    2) At run-time the dynamic type of *bp is examined, since that is Derived
    the actual function that gets called is the overridden version in the
    Derived class, i.e. Derived::eek:perator=(const Base&)

    Derived::eek:perator=(const Derived&) never gets considered at step 1 because
    there is no similar function in the Base class, so it cannot be called at
    step 2.

    john
     
    John Harrison, Jun 14, 2004
    #3
  4. Stephan Kurpjuweit wrote:
    > Hi,
    >
    > could you please help me with the following example?
    >
    >
    > struct Base
    > {
    > virtual Base& operator = (const Base &k) {}
    > };
    >
    > struct Derived: public Base
    > {
    > // 1
    > virtual Derived& operator = (const Derived &k) {}
    >
    > // 2
    > virtual Base& operator = (const Base &k) {}
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > Derived *dp = new Derived();
    > Base *bp = new Derived(*dp);
    >
    > *bp = *dp;
    >
    > return 0;
    > }
    >
    >
    > Why does the assignment in main lead to a call to the second assignment
    > operator (// 2) and not to the first one (//1)?
    > ...


    Because selection of candidate functions for overload resolution is
    based on _static_ type of the object. In this case static type of
    left-hand side of the assignment is 'Base'. This means that only
    'Base::eek:perator=(const Base&)' is considered (and, therefore, chosen) by
    overload resolution.

    Now, since 'Base::eek:perator=(const Base&)' is declared as 'virtual', the
    choice of actual function to call will be based on _dynamic_ type of the
    object. In this case dynamic type of left-hand side of the assignment is
    'Derived', which means that 'Derived::eek:perator=(const Base&)' will be
    called.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 14, 2004
    #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.
Similar Threads
  1. N4M
    Replies:
    17
    Views:
    614
    Daniel T.
    Aug 19, 2004
  2. Krivenok Dmitry
    Replies:
    13
    Views:
    1,445
    Axter
    Jun 1, 2006
  3. Chris
    Replies:
    34
    Views:
    1,531
  4. Replies:
    2
    Views:
    341
    James Kanze
    Apr 13, 2007
  5. sukhpal
    Replies:
    5
    Views:
    358
    James Kanze
    Feb 9, 2009
Loading...

Share This Page