修复C ++多继承模糊调用 [英] Fixing C++ Multiple Inheritance Ambiguous Call
问题描述
我有三个类结构如下:
#include <iostream>
using namespace std;
class Keyword
{
public:
virtual float GetValue() = 0;
};
class CharacterKeyword : public Keyword
{
public:
virtual float GetValue(){return _value;}
private:
float _value;
};
class MeasurementKeyword : public Keyword
{
public:
virtual float GetValue(){return _value;}
private:
float _value;
};
class AddressType : public CharacterKeyword, public MeasurementKeyword
{
private:
float address;
float addresExt;
};
int main()
{
AddressType *a = new AddressType();
a->GetValue();
return 0;
}
我得到以下结果:
在函数'int main()':
错误:请求成员'GetValue' code>
错误:candidate are:virtual float关键字:: GetValue()
错误:virtual float CharacterKeyword :: GetValue()
错误:virtual float CharacterKeyword :: GetValue >
I am getting the following:
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()
我已经读过多个继承,我知道它有很多陷阱 - 这是其中之一。我需要我的类结构是这样的,所以我想知道是否有一种方法,我可以解决这个使用模板?
I have done some reading into multiple inheritance and I know that it has a lot of pitfalls - this being one of them. I need my class structure to be like this so I was wondering if there was a way that I could fix this using templates?
更新
在阅读您的评论后,我原来的想法是,也许我可以在 AddressType
,即 CharacterKeyword
和 AddressType
c $ c> AddressType 的模板
Update
After reading your comments, my original thought was that maybe I can just delineate between an AddressType
that is a CharacterKeyword
and an AddressType
that is a MeasurementKeyword
by templating the AddressType
. And using it as such in the updated code. OR I can just specify the namespace of the member that I would like. Since the templated way has not been mentioned yet as an answer, is it a bad fix? Should I just specify the namespace of the member I want?
template <class T>
class AddressType : public T
{
private:
float address;
float addresExt;
};
int main()
{
AddressType<MeasurementKeyword> *a = new AddressType<MeasurementKeyword>();
a->GetValue();
return 0;
}
推荐答案
a href =http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem>钻石继承模式,要解决错误,您可以指定您希望成员从哪个特定的命名空间。
This is because of a diamond inheritance pattern, to resolve the error you can specify the specific namespace you want the member from like.
paddressType->MeasurementKeyword::GetValue()
或
paddressType->CharacterKeyword::GetValue()
- $ c> AddressType 类可以从它继承的两个类中访问
GetValue
成员,并且不能选择一个(调用是不明确的)。 - 范围解析运算符(
::
)有助于指定您实际需要哪一个。 - 你没有说你真正想要的代码这样做,我只是说通常复杂的继承模式不利于创建可读的代码,重新思考你实际想要的。 / li>
- Basically your
AddressType
class has access to theGetValue
members from both the classes it inherits from and can't choose one (call is ambiguous). - The scope resolution operator (
::
) helps specify which one you actually want. - You haven't said what you actually want this code to do so I'll just say that generally complex inheritance patterns are not conducive to creating readable code, rethink what you actually want.
这篇关于修复C ++多继承模糊调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!