You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

抽象类多层继承下虚函数声明咨询: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.
  • 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).

Which draw() Declaration Should Shape Use?

Given your setup:

  • Transformable is abstract (has mirror() as pure virtual).
  • Shape is an intermediate class, and Circle is intended to be its derived class (I assume there's a missing : public Shape in the Circle definition, otherwise it can't inherit from Shape).
  • Circle implements mirror(), which means Shape didn't implement it—so Shape is 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 own draw() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:17:47