简体   繁体   English

从接口和实现C ++继承

[英]Inheriting from both an interface and an implementation C++

I usually try to find answers here before I post anything, but I'm not even sure how to formulate my question. 在发布任何内容之前,我通常会尝试在此处找到答案,但是我什至不知道如何提出问题。

So here's what I want to do... I want to define a Base Interface, and a Derived Interface. 所以这就是我想要做的...我想定义一个基本接口和一个派生接口。 Then, I want to implement the Base Interface, with extra variables and methods. 然后,我想使用额外的变量和方法来实现基本接口。 Finally, I want to implemented a Derived class, from the implemented Base Interface BUT ALSO from the Derived Interface. 最后,我想从已实现的基础接口中实现一个派生类,但从派生接口中也可以。 I don't know about you, but my head hurts. 我不认识你,但我的头很痛。

If I do something like below, I get Ambiguous definitions under the DerivedFloat code since that code "sees" both the GetBaseValue method from the IBase, inherited through IDerivedFloat, as well as the GetBaseValue inherited from Base. 如果执行以下操作,则会在DerivedFloat代码下获得歧义定义,因为该代码“看到”了IBase的GetBaseValue方法(继承自IDerivedFloat)以及GetBaseValue(继承自Base)。

Surely, there must be a way to derive a class which uses the expanded features of the Base Implementation, as well as making sure it implements the required IDerivedFloat methods. 当然,必须有一种派生类的方法,该类使用基本实现的扩展功能,并确保它实现了所需的IDerivedFloat方法。

Now... This is a dummy example to show what I'm conceptually trying to achieve. 现在...这是一个虚拟的例子,显示了我在概念上试图实现的目标。 It's not a real life example. 这不是现实生活中的例子。

template <typename VALUE_TYPE>
class IBase
{
public:
  virtual VALUE_TYPE GetBaseValue() const = 0;
};

class IDerivedFloat : public IBase<FLOAT>
{
public:
  virtual void SetBaseValue(const FLOAT & value) = 0;
};

// Implementation of Base
template <typename VALUE_TYPE>
class Base : public IBase<VALUE_TYPE>
{
public:
  VALUE_TYPE GetBaseValue() const { return m_BaseValue; }

protected:
  VALUE_TYPE m_BaseValue;
}

// Uses expanded Base AND implements IDerivedFloat
class DerivedFloat : public Base<FLOAT>, public IDerivedFloat
{
public:
   void SetBaseValue(const FLOAT & value) { m_BaseValue = value };
}

You can use virtual inheritance to work around this problem: 您可以使用虚拟继承来解决此问题:

class IDerivedFloat : virtual IBase<FLOAT>
{
public:
  virtual void SetBaseValue(const FLOAT & value) = 0;
};

template <typename VALUE_TYPE>
class Base : virtual IBase<VALUE_TYPE>
{
public:
  VALUE_TYPE GetBaseValue() const { return m_BaseValue; }

protected:
  VALUE_TYPE m_BaseValue;
}

Using virtual inheritance gives the derive class one instance of the base class members, instead of one from each time it exists in the class hierarchy. 使用虚拟继承为派生类提供了基类成员的一个实例,而不是每次在类层次结构中都存在一个实例。

Multiple inheritance is an issue precisely because of the ambiguity issue you ran into, but there are ways to get around it. 多重继承是一个问题,正是因为您遇到了歧义性问题,但是有许多方法可以解决它。 You have to explicitly tell the compiler which super you are calling the ambiguous functions from, by leading the function call with the super's name and a double colon. 您必须通过使用超级名称和双冒号来引导函数调用,从而明确告诉编译器您从哪个超级调用歧义函数。

Example: 例:
- C inherits from A and B. -C从A和B继承。
- A and B both have add() function. -A和B都具有add()函数。
- In C, you have to say A::add() or B::add() to tell the compiler which one to use. -在C语言中,您必须说A :: add()或B :: add()告诉编译器要使用哪个。

Link for details and more complete implementation: http://www.cprogramming.com/tutorial/multiple_inheritance.html 链接以获取详细信息和更完整的实现: http : //www.cprogramming.com/tutorial/multiple_inheritance.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM