有这样一段代码:
class Base
{
public:
virtual ~Base(){};
virtual void show()=0;
};
class Derived:public Base
{
public:
virtual void show(){cout<<"derived show....."<<endl;}
void other_show(){cout<<"derived other_show......"<<endl;}
};
int main()
{
Base *b=NULL;
Derived *d=(Derived *)b;//这里把一个指向纯虚基类指针转换为他的派生类对象,感觉有点不对劲,但运行起来是可以的,一般来说,把指向派生类的指针转换为指向基类的指针是安全的,但是这里的写法好吗?
d->other_show();
} |