作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
class Base
{
public:
virtual void doSomething() = 0;
};
class BaseImpl : public virtual Base
{
public:
virtual void doSomething() {
// something
}
};
class BaseDerived: public virtual Base
{
public:
virtual void doSomething2() = 0;
};
class BaseDerivedImpl: public BaseImpl, public BaseDerived
{
public:
virtual void doSomething2(){
// sonething2
}
};
然后我有
Base* b = new BaseImpl();
b->doSomething(); // fatal error at this line (not in the method, but in the method invocation)
问题是它甚至没有进入函数。
使用这样的层次结构有问题吗?
最佳答案
由于 OP 忽略了评论,所以让我在这里回答问题:
Is it something wrong with using such hierarchy?
不,没有错。这是解决“可怕的钻石”问题的标准方法(实际上并不是那么可怕)。
然而,在这个例子中钻石甚至没有出现:
Base* b = new BaseImpl();
BaseImpl
直接派生自 Base
,因此您拥有标准的单一继承。您的代码的行为就像根本没有定义 BaseDerived
和 BaseDerivedImpl
一样。您可以将它们注释掉,应用程序仍然会崩溃。
然后您在此实例上调用 doSomething
,它崩溃了。 doSomething
的实现如下:
// something
因此,我的结论是 //something
会导致崩溃,但如果不查看该方法的实现就无法判断。
关于c++ - "dreaded diamond"具有多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15525052/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!