P
Philippe Guglielmetti
Look at these few lines of code:
class A { public: virtual void f() { cout << "A";}};
class B : public A{public: static void f() { cout << "B"; }};
class C : public B{public: void f() { cout << "C"; }}; // virtual or not ?
that's the question...
int main(int, char**)
{
A* x; A a; B b; C c;
a.f(); b.f(); c.f();
x = &a; x->f(); x = &b; x->f(); x = &c; x->f();
}
Borland C++ 5.5 outputs "ABCAAA"
MS VC6 and 7 give "ABCAAC"
my explanation is that C::f overrides the virtual A::f with VC,
while C::f overrides the static B::f with BC5. In fact, if C is redefined as
class C : public B{public: virtual void f() { cout << "C"; }};
then BC5 behaves as VC
Questions:
1) what does the standard say? (I was unable to find a clear answer..)
2) what is the reason for allowing the above code to compile without even a
warning ?
class A { public: virtual void f() { cout << "A";}};
class B : public A{public: static void f() { cout << "B"; }};
class C : public B{public: void f() { cout << "C"; }}; // virtual or not ?
that's the question...
int main(int, char**)
{
A* x; A a; B b; C c;
a.f(); b.f(); c.f();
x = &a; x->f(); x = &b; x->f(); x = &c; x->f();
}
Borland C++ 5.5 outputs "ABCAAA"
MS VC6 and 7 give "ABCAAC"
my explanation is that C::f overrides the virtual A::f with VC,
while C::f overrides the static B::f with BC5. In fact, if C is redefined as
class C : public B{public: virtual void f() { cout << "C"; }};
then BC5 behaves as VC
Questions:
1) what does the standard say? (I was unable to find a clear answer..)
2) what is the reason for allowing the above code to compile without even a
warning ?