本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

关联条款 Item 36

接口继承和实现继承不同。在 public 继承下, derived classes 总是继承 base class 的接口

class Shape{
public:
virtual void draw() const = 0;
virtual void error(const std::string &msg);
int objectID() const;
//...
}; class Rectangle: public Shape{...};
class Ellipse: public Shape{...};

1.pure virtual 函数 --> 让 derived classes 仅仅继承函数接口。

Shape::draw 的声明式仍是对详细 derived classes 设计者说。 “你必须提供一个 draw 函数,但我不干涉你怎么实现它”



2.impure virtual 函数 --> 让 derived classes 继承该函数的接口和缺省实现

Shape::error 的声明式告诉 derived classes 设计者说, “你必须支持一个 error 函数,但假设你不想自己写一个,能够使用 Shape要提供的缺省版本号”



3.non-virtual 函数 --> 让 derived classes 继承函数的接口及一份强制性实现



class 设计常犯两个错误

1.将全部函数声明为 non-virtual

Item 7:为多态基类声明 virtual 析构函数

2.将全部成员函数声明为 virtual



经验: impure virtual 函数同一时候指定函数声明和函数缺省行为,可能造成危急。

演示样例:

class AirPort{...};
class AirPlane{
public:
virtual void fly(const AirPort &destination);
//...
};
void AirPort::fly(const AirPort &destination){
//缺省代码。将飞机飞至指定的目的地
}
class ModelA: public AirPlane {...};
class ModelB: public AirPlane {...}; class ModelC: public AirPlane {
... // 未声明 fly 函数, 但C型飞机的飞行方式与A。B不同。
}; AirPort PDX(...);
AirPlane *pa = new ModelC;
//...
pa->fly(PDX); //调用 AirPlane::fly

解析:

这个程序试图以 ModelA, ModelB 的飞行方式来飞 ModelC



纠正:

切断“virtual 函数接口” 和 其“缺省实现” 之间的连接。将 AirPlane::fly 改为一个 pure virtual 函数,仅仅提供飞行接口,

另以独立函数 defaultFly 提供缺省实现。

class AirPlane{
public:
virtual void fly(const AirPort &destination) = 0;
//...
protected: // protected 由于client不须要在意飞机能不能飞
void defaultFly(const AirPort &destination); //Item 36
}; void AirPlane::defaultFly(const AirPlane &destination){
//缺省代码,将飞机飞至指定的目的地
} class ModelA: public AirPlane{
public:
virtual void fly(const AirPort &destination){
defaultFly(destination);
}
//...
}; class ModelB: public AirPlane{
public:
virtual void fly(const AirPort &destination){
defaultFly(destination);
}
//...
}; class ModelC: public AirPlane{
public:
virtual void fly(const AirPort &destination);
//...
}; void ModelC::fly(const AirPort &destination){ // pure virtual 函数迫使 ModelC 必须提供自己的 fly 版本号
//...
}

纠正2:我更喜欢这个,没那么多变量名

class AirPlane{
public:
virtual void fly(const AirPort &destination) = 0;
//...
protected: // protected 由于client不须要在意飞机能不能飞
void defaultFly(const AirPort &destination); //Item 36
}; void AirPlane::fly(const AirPlane &destination){ //pure virtual 函数实现
//缺省代码。将飞机飞至指定的目的地
} class ModelA: public AirPlane{
public:
virtual void fly(const AirPort &destination){
AirPlane::fly(destination);
}
//...
}; class ModelB: public AirPlane{
public:
virtual void fly(const AirPort &destination){
AirPlane::fly(destination);
}
//...
}; class ModelC: public AirPlane{
public:
virtual void fly(const AirPort &destination);
//...
}; void ModelC::fly(const AirPort &destination){ // pure virtual 函数迫使 ModelC 必须提供自己的 fly 版本号
//...
}

Effective C++ Item 34 区分接口继承与实现继承的更多相关文章

  1. 读书笔记 effective c++ Item 34 区分接口继承和实现继承

    看上去最为简单的(public)继承的概念由两个单独部分组成:函数接口的继承和函数模板继承.这两种继承之间的区别同本书介绍部分讨论的函数声明和函数定义之间的区别完全对应. 1. 类函数的三种实现 作为 ...

  2. Effective C++ 34 区分接口继承和实现继承

    public继承从根本上讲,有两部分:接口继承和实现继承.两者之前的区别很像函数声明与函数定义. 具体设计中,会呈现三种形式:derived class只继承成员函数的接口(纯虚函数):derived ...

  3. 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用

    1. 什么样的接口才是好的接口 C++中充斥着接口:函数接口,类接口,模板接口.每个接口都是客户同你的代码进行交互的一种方法.假设你正在面对的是一些“讲道理”的人员,这些客户尝试把工作做好,他们希望能 ...

  4. Effective C++ Item 36 绝不又一次定义继承而来的 non-virtual 函数

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:绝对不要又一次定义继承而来的 non-virtual 函数 --> Item 7 ...

  5. Effective C++ Item 34 Differentiate between inheritance of interface and inheritance of implementation

    1. 成员函数的接口总是被继承. 如 Item32 所说, public 意味着 is-a, 所以对 base class 为真的任何事情对 derived class 也为真 2. 声明一个 pur ...

  6. Effective C++ Item 37 绝不又一次定义继承而来的缺省參数值

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:绝对不要又一次而来的缺省參数值.由于缺省參数值都是静态绑定,而 virtual 函数 ...

  7. Effective JavaScript Item 34 在prototype上保存方法

    本系列作为EffectiveJavaScript的读书笔记. 不使用prototype进行JavaScript的编码是全然可行的,比如: function User(name, passwordHas ...

  8. Effective C++ -----条款34:区分接口继承和实现继承

    接口继承和实现继承不同.在public继承之下,derived classes总是继承base class的接口. pure virtual函数只具体指定接口继承. 简朴的(非纯)impure vir ...

  9. Effective C++:规定34:区分接口继承和实现继承

    (一个) class Shape { public: virtual void draw() const = 0; virtual void error(const string& msg); ...

随机推荐

  1. 【模拟】Friday the Thirteenth

    题目描述 Is Friday the 13th really an unusual event?That is, does the 13th of the month land on a Friday ...

  2. 使用gtest自动化测试并给出性能测试结果(windows 版本,版本平台也可以使用,但并没有做完整的测试)

    /************************************************************* *使用gtest自动化测试 * ********************* ...

  3. sql server 存储过程中使用变量表,临时表的分析(续)

    最近,我有一朋友,对我说他的数据库中的很多存储过程,执行都是超时.让我替他看看是什么原因.我一看,原来他的存储过程中用了很多的临时表与变量表.于是我跟他说过犹不及. 在存储过程中使用临时表或变量表,使 ...

  4. Linux的五个查找命令:find,locate,whereis,which,type 及其区别

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  5. linux之openssh协议

    SSH的全称是Secure Shell,简单说来ssh是一种安全的外壳协议,用于两个计算机间安全的远程登陆,说它安全,是因为ssh采用公钥加密的机制.最开始时用作远程管理的工具是telnet,这个协议 ...

  6. react数组key的唯一性

    1.不要使用数组的index索引作为key 2.在相邻的元素间,一定确保key的唯一性,如果出现了相同的 key,会抛出一个 Warning,告诉相邻组件间有重复的 key 值.并且只会渲染第一个重复 ...

  7. namespace使用方法

    https://blog.csdn.net/CHIERYU/article/details/50262043 参考值这文献

  8. INTZ DX format

    http://aras-p.info/texts/D3D9GPUHacks.html 格式 用法 资源 描述 NVIDIA GeForce AMD Radeon 英特尔 阴影映射 D3DFMT_D16 ...

  9. Linux学习之二-Linux系统的目录结构

    Linux学习之二-Linux系统的目录结构 在Linux的根目录下,有很多的目录,但是需要记住,对于Linux而言,一切皆文件.因此此处的目录也是文件.用ls / 命令就能看到根目录下的各类不同的目 ...

  10. ECSHOP站内页面跳转,避免死链

    2.x版本域名重定向: # For ISAPI_Rewrite 2.x RewriteCond Host: ^steveluo\.name$ RewriteRule (.*) http\://www\ ...