mother
对象如何在调用类型为son
时能够调用father
的函数?带多重继承的C++类型转换
这里的father
对象没有创建,那么如何来调用drive()
函数。对于新手的问题抱歉。
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class father
{
public:
void drive(){cout<<"Driving";}
};
class mother
{
public:
void cook(){cout<<"Cooking";}
};
class son: public father, public mother
{
};
int main() {
// your code goes here
mother *m = new mother();
son* s = static_cast<son *>(m);
s->drive();
return 0;
}
使用旧stlye强制转让给你没有办法验证你得到 – doctorlove
这是非法的。它可能会出现**工作,但它不会。 – BoBTFish
@Coder'(son *)'强制编译器接受它自己不会“推断”的内容。 – Scheff