Jerry Coffin
10/4/2008 9:48:00 PM
In article <48e7bf49$0$28914$9b4e6d93@newsspool1.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.