尝试使用虚拟继承解决钻石问题
Diamond issue when trying to resolve it using virtual inheritance
我是C++的新手,在解决钻石问题时我很差劲:这里的代码:
#include <iostream>
using namespace std;
// Base class
class Base
{
public:
virtual void getArea()
{
cout << "Called by Basen";
}
};
// Derived class
class Rectangle: public virtual Base
{
public:
void getArea()
{
cout << "Called by Rectanglen";
}
};
// Derived class
class Square: public virtual Base
{
public:
void getArea()
{
cout << "Called by Squaren";
}
};
// Derived class
class Triangle: public Rectangle, Square
{
public:
void blabla(){}
};
int main(void)
{
Triangle Tri;
Tri.getArea();
return 0;
}
我得到g++错误:
main.cpp:36:7: error: no unique final overrider for ‘virtual void Base::getArea()’ in ‘Triangle’
class Triangle: public Rectangle, Square
^
main.cpp: In function ‘int main()’:
main.cpp:45:6: error: request for member ‘getArea’ is ambiguous
Tri.getArea();
^
main.cpp:29:12: note: candidates are: virtual void Square::getArea()
void getArea()
^
main.cpp:19:12: note: virtual void Rectangle::getArea()
void getArea()
我在网上发现虚拟继承解决了这个问题,那么我的错误是什么呢。
提前感谢
作为编译器思考:应该调用什么方法?
你有一个三角形,既是正方形又是矩形,你问的是面积。您的三角形应该选择使用Rectangle.getArea()
还是Square.getArea()
?
编译器无法知道。这里的解决方案是覆盖getArea()方法,例如:
class Triangle: public Rectangle, Square
{
public:
void blabla(){}
void getArea()
{
Square::getArea; //I'm a square
Rectange::getArea; //I'm a rectangle
}
};
即使没有基类,这个问题也会被触发。使用钻石继承的一个例子如下:
class Base
{
protected:
int x;
};
class Derived1: virtual public Base
{
//some stuff
};
class Derived2: virtual public Base
{
//some stuff
};
class Join: public Derived1, public Derived2
{
int getX(){
return x;
}
};
在这里,使用虚拟继承允许我们在Join实例中只有基类的一个实例,而不是2个没有并且应该选择x的错误。
您的问题是,在相同的继承级别上有两个不同的类,它们提供了虚拟方法的不同重写。编译器无法知道哪一个应该优先,因此您需要提供自己的重写并执行任何您想要的操作(同时调用其中一个函数,或者滚动您自己的实现)。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- visual c++,如何获取解决方案目录中的代码
- C++如何解决钻石问题?
- C++解决没有虚拟继承的钻石继承问题
- 如何解决钻石问题的这种歧义
- 解决QT类的特定C 钻石问题
- 虚拟继承如何解决 c++ 中的多重继承(钻石)?它将走哪条路
- 为什么使用声明不能解决钻石问题?
- 尝试使用虚拟继承解决钻石问题
- C++解决钻石问题
- 钻石继承的解决方案,但它合适吗