为什么这段代码"not ambigious!" - 虚函数
Why is this piece of code "not ambigious!" - virtual functions
为什么下面的代码不含糊,以及它是如何正常工作的?
#include <QCoreApplication>
#include <iostream>
using namespace std;
class Shape{
public:
virtual void drawShape(){
cout << "this is base class and virtual functionn";
}
};
class Line : public Shape{
public:
virtual void drawShape(){
cout << "I am a linen";
}
};
class Circle : public Shape{
public:
virtual void drawShape(){
cout <<" I am circlen";
}
};
class Child : public Line, public Circle{
public:
virtual void drawShape(){
cout << "I am child :)n";
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Shape *s;
//Line l;
Child ch;
//s = &l;
//s = &ch;
ch.drawShape(); // this is ambiguous right? but it executes properly!
//s->drawShape();
return a.exec();
}
这并不含糊,因为Child
定义了它自己对drawShape
的覆盖,而ch.drawShape
将调用该函数。如果Child
没有提供对drawShape
的覆盖,那么该调用将是不明确的。
相关文章:
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- OpenCV Android C++ imwrite not found
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 错误"Could not find Boost"(缺少:上下文标头)
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- Is !NaN not a NaN?
- 为什么我会" void value not ignored as it ought to be"?
- 解决"ld: library not found for -ltensorflow_framework.2.3.0"
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- Directx 11 - CompileFromFile() is not compiling
- 方法错误"not all control paths return a value"和方法不返回值
- Centos7 g++ "to_string is not in a member of std"
- 将系数存储在头文件的数组中("does not name a type"错误)
- QGraphicsItems not showing up QGraphicsScene
- 在调试模式下引发C++ "deque iterator not dereferencable"异常
- InitializeCriticalSectionEx Not Located In KERNEL32.Dll
- 为什么这段代码"not ambigious!" - 虚函数