我知道该怎样virtual
在成员函数的上下文中工作,但我在网上看到一篇关于虚拟成员类的文章,这让我感到困惑。
我找到的例子是这样的:
class Machine
{
void run () {}
virtual class Parts
{
};
};
// The inner class "Parts" of the class "Machine" may return the number of wheels the machine has.
class Car: public Machine
{
void run() {
cout << "The car is running." << endl;
}
class Parts
{
int get_Wheels () {
cout << "A car has 4 wheels." << endl;
return 4;
}
string get_Fuel_Type () {
cout << "A car uses gasoline for fuel." << endl;
return "gasoline";
}
};
};
文章位于https://en.wikipedia.org/wiki/Virtual_class https://en.wikipedia.org/wiki/Virtual_class claims:
任何 Machine 类的对象都可以用同样的方式访问。程序员可以询问轮子的数量(通过调用 get_Wheels()),而无需知道它是什么类型的机器、该机器有多少个轮子或机器的所有可能类型。像 get_Fuel_Type() 这样的函数可以由派生类 Car 添加到虚拟类 Parts 中。
怎样才能打电话get_Wheels()
或成员类中的任何其他函数Parts
from a Machine*
?看来你必须知道什么样的Machine
在可以打电话之前你有get_wheels()
因为你不能保证该函数有一个实现。
您发布的代码不是 C++,因为该语言不支持虚拟课程在你描述的概念中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)