C++核心准则边译边学-I.25 在类体系中将抽象类定义成接口会更好

共 1535字,需浏览 4分钟

 ·

2019-10-27 23:23

de7d5eb61bb92f324efe4fbb5ab46d8c.webp

I.25: Prefer abstract classes as interfaces to class hierarchies(在类体系中将抽象类定义为接口会更好

译者注:

  1. 抽象类:不能直接生成示例的基类。

  2. 接口:不包含数据成员的抽象类。

Reason(原因)

Abstract classes are more likely to be stable than base classes with state.

抽象类有很大的可能比包含状态的基类更稳定。

译者注:结合全文,这里的抽象类应该指接口。

Example, bad(反面示例)

You just knew that Shape would turn up somewhere :-)

你只知道Shape会在某处表示:-)

class Shape {  // bad: interface class loaded with datapublic:    Point center() const { return c; }    virtual void draw() const;    virtual void rotate(int);    // ...private:    Point c;    vector outline;    Color col;};

This will force every derived class to compute a center -- even if that's non-trivial and the center is never used. Similarly, not every Shape has a Color, and many Shapes are best represented without an outline defined as a sequence of Points. Abstract classes were invented to discourage users from writing such classes:

这将强迫每个派生类都要计算中心位置-哪怕是那些特殊且中心从未不用的图形。类似的,也不是所有的图形都有颜色,很多图形最好不要包含一个有Points序列定义的轮廓线。发明抽象类就是为了鼓励用户写出下面的类:

class Shape {    // better: Shape is a pure interfacepublic:    virtual Point center() const = 0;   // pure virtual functions    virtual void draw() const = 0;    virtual void rotate(int) = 0;    // ...    // ... no data members ...    // ...    virtual ~Shape() = default;};

Enforcement(实施建议)

(Simple) Warn if a pointer/reference to a class C is assigned to a pointer/reference to a base of C and the base class contains data members.

(简单)如果指向类C的指针或引用被复制被C的基类类型的指针或引用,而且基类包含数据成员时,报警。

译者注:也就是说,大师的意见是:基类基本上抽象类就不要有数据成员了。


浏览 99
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报