本文为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. Python的网络编程[3] -> BOOTP 协议[0] -> BOOTP 的基本理论

    BOOTP协议 / BOOTP Protocol 目录 基本理论 BOOTP 与 DHCP 通信流程 数据报文格式 报文加解码实现 1. 基本理论 / Basic Theory BOOTP(Boots ...

  2. MySQL判断中文字符的方法(转)

    准备: 2.1.环境 MySQL mysql> SHOW VARIABLES LIKE "%version%"; +-------------------------+--- ...

  3. flex skin

    原文地址:http://blog.163.com/to_yht/blog/static/1227257742009741359326/当程序做完后,你是否还在为老套的flex组件默认效果发愁?是否还在 ...

  4. DirectoryServicesCOMException

    捕捉到 System.DirectoryServices.DirectoryServicesCOMException Message=该服务器不愿意处理该请求. Source=System.Direc ...

  5. EasyMvc入门教程

    EasyMvc 希望实现的目标:模块化,快速简单化,满足80%的常见需求.基于.Net Core 2.0.5开发.开发环境:VS2017,运行环境支持Window/Linux. 相关链接: 演示地址: ...

  6. 使apache的日志文件里不记录图片文件

    找到: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-A ...

  7. Playonlinux

    apt-get install playonlinux -y apt-get install winbind -y apt-get install unzip -y 开始中搜索:playonlinux ...

  8. thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found

    thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (- ...

  9. Hive 脚本执行

    hive执行脚本 hive -e “sql语句” 会将查询的结果打印在控制台上.  hive -e “sql语句” >> xxx 会将查询的结果重定向到xxx文件中,会显示OK和抓取的数据 ...

  10. zabbix主机自动发现和监控

    在主机较多的时候,配置主机自动发现并加入监控可以代替手动的添加主机,减轻工作量,自动发现由服务端主动发起,Zabbix Server开启发现进程,定时扫描局域网中IP服务器.设备.可以根据需要,在对主 ...