[英]Pure virtual functions and multiple inheritance
I have a (guaranteed tree-shaped) cascade of classes like 我有一个(保证的树形)级联类
class rock_bottom {
...
virtual foo() = 0;
};
class A : rock_bottom {...}
class intermediate: rock_bottom {...}
class B : intermediate {...}
class higher_up {...}
class C : higher_up {...}
I want to implement foo() in the lettered classes A, B, C. Where do I need to declare foo? 我想在字母类A,B,C中实现foo()。在哪里需要声明foo? (Virtual? Pure?) Do I need (trivial) implementations in the middle layers intermediate and higher_up? (虚拟的?纯的?)我是否需要在中间层中级和Higher_up的(简单的)实现?
Question: 题:
I want to implement foo() in the lettered classes A, B, C. Where do I need to declare
foo
? 我想在字母类A,B,C中实现foo()。我需要在哪里声明foo
? (Virtual? Pure?) (虚拟的?纯净的?)
Answer: 回答:
That question needs to be asked in reverse. 这个问题需要反过来提出。 In which base class does it make sense to create the interface foo
. 在哪个基类中创建接口foo
才有意义。 Let's take some classes that you might find in application. 让我们来看一些您可能在应用程序中找到的类。
struct Shape {};
struct Rectangle : public Shape {};
struct Square : public Rectangle {};
struct Ellipse : public Shape {};
struct Circle : public Ellipse {};
It makes sense that all Shape
be able to return their area and perimeter. 所有Shape
都可以返回其面积和周长,这是有道理的。 So you put them as virtual
member functions in Shape
. 因此,您将它们作为virtual
成员函数放在Shape
。
struct Shape
{
virtual double area() const = 0;
virtual double perimeter() const = 0;
};
Before you can create an instance of sub-type of Shape
, you must implement them. 在创建Shape
的子类型实例之前,必须实现它们。
If you want to be able to create a Rectangle
, those functions must be implemented in Rectangle
since there are no other intermediate classes. 如果您希望能够创建Rectangle
,则这些功能必须在Rectangle
实现,因为没有其他中间类。
If you want to be able to create a Square
, those functions must be implemented either in Rectangle
, Square
, or both. 如果您希望能够创建Square
,则必须在Rectangle
, Square
或两者中实现这些功能。
Similarly, if you want to be able to create a Ellipse
, those functions must be implemented in Ellipse
since there are no other intermediate classes. 同样,如果您希望能够创建Ellipse
,则这些功能必须在Ellipse
实现,因为没有其他中间类。
If you want to be able to create a Circle
, those functions must be implemented either in Ellipse
, Circle
, or both. 如果您希望能够创建Circle
,那么必须在Ellipse
, Circle
或两者中都实现这些功能。
However, only Rectangle
s have length and width. 但是,只有Rectangle
具有长度和宽度。 Only Ellipse
s have major radius and minor radius. 只有Ellipse
才具有长半径和短半径。 It doesn't make sense to add virtual member functions in Shape
to return those values. 在Shape
添加虚拟成员函数以返回这些值没有意义。
Question: 题:
Do I need (trivial) implementations in the middle layers intermediate and higher_up? 我是否需要在中间层中级和Higher_up中实现(琐碎的)实现?
Answer: 回答:
No, you don't. 不,你没有。 The answer to the previous question should clarify that more. 上一个问题的答案应对此进行更多说明。
Each override needs to be declared in the class that implements it; 每个覆盖都需要在实现它的类中声明; in your case, A
, B
and C
, since you say each will provide an override. 在您的情况下, A
, B
和C
,因为您说每个都会提供覆盖。
There's no need to declare it in any intermediate class, assuming you're happy for them to remain abstract. 假设您对它们保持抽象感到满意,则无需在任何中间类中声明它。 They inherit the pure function from rock_bottom
, and there's no point overriding that with another pure function. 他们从rock_bottom
继承了纯函数,而用另一个纯函数覆盖它是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.