#include "stdio.h"
class B
{
char c;
public:
B( char n){c = n;}
virtual ~B(void)
{
X();
Y();
}
virtual void X(void)
{
printf("%c",c);
}
void Y(void)
{
printf("%c",c-1);
}
};
class D:public B
{
char c;
public:
D(char n):B(n+1){c = n;}
~D(void)
{
X();
Y();
}
void X(void)
{
printf("%c",c);
}
virtual void Y(void)
{
printf("%c",c-1);
}
};
B *newB(char c)
{
return new D(c);
}
int main(int argc, char* argv[])
{
B *pb= newB('X');
pb->X();
pb->Y();
delete pb;
return 0;
}
结果输出是:XXXWYX ,而不是:XXXXYX
为什么?