inheritance in C++ important question [message #8081 is a reply to message #8079] |
Mon, 16 March 2009 14:09 |
Anonymous Poster
|
|
From: 141.39.167*
|
|
Hi,
what I stated in my last reply in this thread about virtual in C++ turns out to be true. Please compile and run the following snipplet
//---------------
#include<iostream>
class A{
public:
virtual void doit(){std::cout<<"A"<<std::endl;}
};
class B : public A{
public:
void doit(){std::cout<<"B"<<std::endl;}
};
class C{
public:
void doit(){std::cout<<"C"<<std::endl;}
};
class D : public C{
public:
void doit(){std::cout<<"D"<<std::endl;}
};
int main(){
A* x = new B;
C* y = new D;
x->doit();
y->doit();
((D*)y)->doit();
}
//---------------
It prints
B
C
D
and the compiler gives no warning, also not with -Wall -pedantic. Does anybody know how I can force the compiler to not allow the overwriting at all, like in the case of class D where I (one could mistakenly think) overwrite the method?
Please help a C++ fool!
Cheers, Christian
|
|
|