问题标签 [diamond-problem]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - 修复 C++ 多重继承歧义调用
我有三个这样的类:
我得到以下信息:
In function ‘int main()’:
error: request for member ‘GetValue’ is ambiguous
error: candidates are: virtual float Keyword::GetValue()
error: virtual float MeasurementKeyword::GetValue()
error: virtual float CharacterKeyword::GetValue()
我对多重继承做了一些阅读,我知道它有很多陷阱——这就是其中之一。我需要我的类结构是这样的,所以我想知道是否有一种方法可以使用模板来解决这个问题?
更新
阅读您的评论后,我最初的想法是,也许我AddressType
可以CharacterKeyword
通过AddressType
模板MeasurementKeyword
化AddressType
. 并在更新的代码中使用它。或者我可以只指定我想要的成员的命名空间。由于尚未提及模板化方式作为答案,这是一个不好的解决方法吗?我应该只指定我想要的成员的命名空间吗?
c++ - C++ Diamond-like inheritance
I have a class Channel with two two properties, direction and size which are fixed during construction. Direction can take only one of two values, forward (1) or backward(-1). Size can take any value, but there is a physically meaningful distinction between 0 and any nonzero value.
I'd like to be able to write functions that accept Channel objects with known values for the direction and/or size, and I thought to implement this using derived classes:
Obviously I didn't draw all of the permutations.
I tried implementing it as so
Instantiating ForwardChannel and ZeroChannel works fine. Instantiating ForwardZeroChannel calls only the default constructor for Channel which doesn't set the values. I have to add Channel(1, 0) to the initializer list:
but that seems to defeat some of the purpose of deriving from ForwardChannel and ZeroChannel. Is there a better way of doing this?
c++ - C ++中的多重继承,它看起来像钻石,但它不是
C++中的一个多重继承问题:
它看起来像一个钻石问题,但事实并非如此。不能通过添加虚拟继承来解决。
我对吗?
c++ - C++ 中的 Diamond 继承代码未按预期工作
我试图理解 C++ 中菱形问题(多重继承)的解决方案。
我编写了这段代码以更好地理解解决方案,但它的行为与预期不符。
Visual Studio 2008 速成版大喊:错误 C2385:“打印”1> 的模糊访问可能是基础“B”中的“打印”1>,或者可能是基础“C”中的“打印”错误 C3861:“打印” : 未找到标识符
谁能告诉我我在这里缺少什么?
c++ - c++ 虚拟钻石继承
假设我们得到了一个菱形继承,其中 D 从 B1 和 B2 继承,其基数为 V,如下所示:
当我初始化 D* 参数时,我希望 B1 和 B2 默认构造函数调用 V 构造函数。但是,当我运行下一行时, V 被调用了一次。为什么?
提前致谢。
c++ - 钻石继承 - 将叶类对象作为参数传递给根类
我有一个具有以下继承结构的程序
在List
类(完全抽象)中,我有一个纯虚函数
我已经在DoublyLinkedList
andCircularlyLinkedList
类中覆盖了它。
为了解决类中的歧义,我使用范围解析运算符明确指定要继承CircularlyDoublyLinkedList
哪个版本的函数,例如:
我的问题是这个语句insert()
::
DoublyLinkedList::insert(..)
抛出错误
当我将声明更改为
我收到另一个错误,因为insert(...)
接受类型为参数的参数List**
如何在没有强制转换的情况下解决此问题?
java - 为什么java不支持多重继承,尽管所有java方法默认都是虚拟的?
为什么Java不支持多重继承,尽管所有java方法默认都是虚拟的?为什么Java中会出现钻石问题?
c++ - 为什么 GCC 给我一个错误:没有唯一的最终覆盖?
在下面的代码中,我收到以下警告和错误:
但是,如果我从 A 中删除 B 的虚拟继承(即struct B : public A
),我只会收到警告,没有错误。
为什么?这是可怕的钻石吗?
c++ - 带有 boost::bind 的钻石继承
我有这样的设计:
编译器抱怨第一次 boost::bind 调用。C 和 D 内部的函数调用没有问题。这是确切的错误:
boost/bind/mem_fn_template.hpp(384) : 错误 C2594: 'newline' : 从 'Concrete *' 到 'A *' 的模糊转换 [T=TConcrete]
任何想法这可能有什么问题?
java - 澄清多重继承的概念:会出现菱形结构问题吗?
假设你有两个接口 I1 和 I2 两个接口都有相同的方法
现在一个类同时实现了 I1 和 I2。
注意:我知道如果您实现多个具有相同方法的接口,那么实现类中将只有一个实现。
编译器是否根据 throws 子句推断此方法是来自 I1 还是来自 I2?是否需要方法签名说明?(我知道签名包括名称和传递的参数。)
如果我们已经实现了这两个接口,然后我们只抛出 e1 ,编译器是否会允许这样做,因为实现类并没有真正实现这两个接口?会不会导致钻石结构问题?
我们是否需要使用 throws 子句 e1 和第二次 e2 实现相同的方法?当我尝试这个时,编译器告诉我我有重复的方法。
如果我们先实现 I1,那么它说它是 I1 的方法,如果我们先写 I2,那么它说它是 I2 的方法。解释为什么会这样。