Re: [Algorithms] C++ inherited constructors
Brought to you by:
vexxed72
|
From: Peter D. <pd...@mm...> - 2000-07-16 12:17:33
|
> You can't call virtual functions from constructors. Doing so is
> undefined in C++. (MSVC will call the local instantiation, even when
> there's an overloaded one, for instance.)
Actually you can, and it's well defined. The problem is that in the
constructor the static and the dynamic type of "this" are the same.
#include <iostream>
struct A
{
virtual void f() const { std::cout << "A::f()\n"; }
};
void g(A const & a)
{
a.f();
}
struct B: A
{
B() { g(*this); }
virtual void f() const { std::cout << "B::f()\n"; }
};
struct C: B
{
virtual void f() const { std::cout << "C::f()\n"; }
};
int main()
{
C c;
return 0;
}
--
Peter Dimov
Multi Media Ltd.
|