dynamic binding issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christof Warlich

    #1

    dynamic binding issue

    Hi,

    obviously, I'm missing something w.r.t. dynamic binding in C++:

    Can anyone explain why the code below prints "Base" instead of
    "Derived" when calling the Base constructor? I'm calling f() on
    a Derived instance in the Base constructor, right? So why isn't
    the f() of Derived called as it is in main()?

    Can I work around this to get the behaviour that I expected?

    Thanks,

    Christof

    #include <iostream>
    #include <typeinfo>
    class Base {
    public:
    Base(Base *x) {
    x->f(); // why base??
    }
    virtual void f(void) {
    std::cout << "Base\n";
    }
    };
    class Derived:public Base {
    public:
    Derived():
    Base(this) {
    }
    void f(void) {
    std::cout << "Derived\n" ;
    }
    };
    int main(void) {
    Derived derived;
    Base &base = derived;
    base.f(); // ok, derived
    }
  • red floyd

    #2
    Re: dynamic binding issue

    Christof Warlich wrote:
    Hi,
    >
    obviously, I'm missing something w.r.t. dynamic binding in C++:
    >
    Can anyone explain why the code below prints "Base" instead of
    "Derived" when calling the Base constructor? I'm calling f() on
    a Derived instance in the Base constructor, right? So why isn't
    the f() of Derived called as it is in main()?
    >
    Can I work around this to get the behaviour that I expected?
    >
    Thanks,
    This is FAQ 23.5


    Essentially, the reason is that the Derived portion hasn't been
    constructed yet.
    #include <iostream>
    #include <typeinfo>
    class Base {
    public:
    Base(Base *x) {
    x->f(); // why base??
    }
    virtual void f(void) {
    std::cout << "Base\n";
    }
    };
    class Derived:public Base {
    public:
    Derived():
    Base(this) {
    }
    void f(void) {
    std::cout << "Derived\n" ;
    }
    };
    int main(void) {
    Derived derived;
    Base &base = derived;
    base.f(); // ok, derived
    }

    Comment

    • Christof Warlich

      #3
      Re: dynamic binding issue

      red floyd schrieb:
      This is FAQ 23.5

      >
      Essentially, the reason is that the Derived portion hasn't been
      constructed yet.
      Thanks for pointing me to this. 23.6 explains some workarounds.

      Comment

      • Jerry Coffin

        #4
        Re: dynamic binding issue

        In article <48e7bf49$0$289 14$9b4e6d93@new sspool1.arcor-online.net>,
        cwarlich@gmx.de says...
        Hi,
        >
        obviously, I'm missing something w.r.t. dynamic binding in C++:
        >
        Can anyone explain why the code below prints "Base" instead of
        "Derived" when calling the Base constructor? I'm calling f() on
        a Derived instance in the Base constructor, right? So why isn't
        the f() of Derived called as it is in main()?
        >
        Can I work around this to get the behaviour that I expected?
        While a constructor is running, the type (the ONLY) type of that object
        is the type of the class of that constructor. IOW, while the base class
        ctor is running, the the type of the object is the base class, so when
        you call a virtual function, that's the class' virtual function that
        will be called.

        --
        Later,
        Jerry.

        The universe is a figment of its own imagination.

        Comment

        Working...