[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

dynamic binding issue

Christof Warlich

10/4/2008 7:09:00 PM

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
}
3 Answers

red floyd

10/4/2008 7:40:00 PM

0

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
http://parashift.com/c++-faq-lite/strange-inheritance.htm...

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
> }

Christof Warlich

10/4/2008 8:14:00 PM

0

red floyd schrieb:
> This is FAQ 23.5
> http://parashift.com/c++-faq-lite/strange-inheritance.htm...
>
> Essentially, the reason is that the Derived portion hasn't been
> constructed yet.

Thanks for pointing me to this. 23.6 explains some workarounds.

Jerry Coffin

10/4/2008 9:48:00 PM

0

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.