抽象类多层继承下虚函数声明咨询:Shape类draw函数选型
Hey there! Let's walk through this C++ inheritance and virtual function question clearly. First, I'll lay out the rules for virtual functions when working with abstract base classes and intermediate layers, then we'll pick the right draw() declaration for your Shape class.
Original Code Structure
struct Transformable { virtual void mirror()=0; } class Shape: public Transformable { Position position; Color color; public: virtual void draw()=0; // 0. <-此选项,或 virtual void draw(); // I. <-此选项,或 void draw(); // II. <-此选项,或 // III. <-此选项? } class Circle { // Note: This should inherit from Shape to act as "derived class C" double radius; public: void mirror(); }
Rules for Virtual Functions in Abstract Class Inheritance Chains
Let's break down how virtual functions behave when passing through an intermediate class between an abstract base and a concrete derived class:
- For pure virtual functions from the abstract base (e.g.,
Transformable::mirror):- If the intermediate class (
Shape) needs to be instantiable (non-abstract), it must implement all inherited pure virtual functions. - If the intermediate class only exists as a base for further derivation (no need to be instantiated), it can skip implementing the pure virtual function—this automatically makes the intermediate class abstract, forcing derived classes (like
Circle) to implement the function instead. You can also explicitly re-declare the function as pure virtual (virtual void mirror()=0;) for clarity, but it's not required.
- If the intermediate class (
- For new virtual functions in the intermediate class (e.g.,
Shape::draw):- If you want all derived classes to mandatorily implement this function, declare it as a pure virtual function (
=0). This keeps the intermediate class abstract (even if it implemented all base pure virtual functions). - If you want derived classes to optionally override the function (with a default implementation in the intermediate class), declare it as a regular virtual function (
virtual void draw();) and provide an implementation. Skipping the implementation here leaves the function undefined—calling it directly will cause a linker error, though the class remains abstract if other pure virtual functions exist. - Avoid non-virtual declarations (option II) or omitting the function entirely (option III)—this breaks polymorphism, since derived class implementations won't override the base class function (they'll just hide it).
- If you want all derived classes to mandatorily implement this function, declare it as a pure virtual function (
Which draw() Declaration Should Shape Use?
Given your setup:
Transformableis abstract (hasmirror()as pure virtual).Shapeis an intermediate class, andCircleis intended to be its derived class (I assume there's a missing: public Shapein theCircledefinition, otherwise it can't inherit fromShape).Circleimplementsmirror(), which meansShapedidn't implement it—soShapeis already abstract.
The correct choice is option 0: virtual void draw()=0;. Here's why:
- It enforces that every concrete shape class (like
Circle) must provide its owndraw()implementation, which is logical for a shape hierarchy—all shapes need to know how to render themselves. - It explicitly marks
draw()as a required interface, making the code's intent clear to other developers. - Options I, II, and III either break polymorphism, leave ambiguity, or fail to enforce the necessary implementation in derived classes.
内容的提问来源于stack exchange,提问作者Kabir
相关产品推荐
相关产品推荐

