Effective C++
  • 介绍
  • 0.导读
  • 1.让自己习惯 C++
    • 条款01:视 C++ 为一个语言联邦
    • 条款02:尽量以 const, enum, inline 替换 #define
    • 条款03:尽可能使用 const
    • 条款04:确定对象被使用前已先被初始化
  • 2.构造/析构/赋值运算
    • 条款05:了解 C++ 默默编写并调用哪些函数
    • 条款06:若不想使用编译器自动生成的函数,就该明确拒绝
    • 条款07:为多态基类声明 virtual 析构函数
    • 条款08:别让异常逃离析构函数
    • 条款09:绝不在构造和析构过程中调用 virtual 函数
    • 条款10:令 operator= 返回一个reference to *this
    • 条款11:在 operator= 中处理自我赋值
    • 条款12:复制对象时勿忘其每一个成分
  • 3.资源管理
    • 条款13:以对象管理资源
    • 条款14:在资源管理类中小心 copying 行为
    • 条款15:在资源管理类中提供对原始资源的访问
    • 条款16:成对使用 new 和 delete 时要采用相同形式
    • 条款17:以独立语句将 newed 对象置入智能指针
  • 4.设计与声明
    • 条款18:让接口容易被正确使用,不易被误用
    • 条款19:设计 class 犹如设计 type
    • 条款20:宁以 pass-by-reference-to-const 替换 pass-by-value
    • 条款21:必须返回对象时,别妄想返回其 reference
    • 条款22:将成员变量声明为 private
    • 条款23:宁以 non-member、non-friend 替换 member 函数
    • 条款24:若所有参数皆需类型转换,请为此采用 non-member 函数
    • 条款25:考虑写出一个不抛异常的 swap 函数
  • 5.实现
    • 条款26:尽可能延后变量定义式的出现时间
    • 条款27:尽量少做转型
    • 条款28:避免返回 handles 指向对象内部成分
    • 条款29:为”异常安全“而努力是值得的
    • 条款30:透彻了解 inlining 的里里外外
    • 条款31:将文件间的编译依存关系降至最低
  • 6.继承与面对对象设计
    • 条款32:确定你的 public 继承塑模出 is-a 关系
    • 条款33:避免遮掩继承而来的名称
    • 条款34:区分接口继承和实现继承
    • 条款35:考虑 virtual 函数以外的其他选择
    • 条款36:绝不重新定义继承而来的 non-virtual 函数
    • 条款37:绝不重新定义继承而来的缺省参数值
    • 条款38:通过复合塑模出 has-a 或”根据某物实现出“
    • 条款39:明智而审慎地使用 private 继承
    • 条款40:明智而审慎地使用多重继承
  • 7.模板与泛型编程
    • 条款41:了解隐式接口和编译期多态
    • 条款42:了解 typename 的双重含义
    • 条款43:学习处理模板化基类内的名称
    • 条款44:将与参数无关的代码抽离 templates
    • 条款45:运用成员函数模板接收所有兼容类型
    • 条款46:需要类型转换时请为模板定义非成员函数
    • 条款47:请使用 traits classes 表现类型信息
    • 条款48:认识 template 元编程
  • 8.定制 new 和 delete
    • 条款49:了解 new-handler 的行为
    • 条款50:了解 new 和 delete 的合理替换时机
    • 条款51:编写 new 和 delete 时需固守常规
    • 条款52:写了 placement new 也要写 placement delete
  • 9.杂项讨论
    • 条款53:不要请忽编译器的警告
    • 条款54:让自己熟悉包括 TR1 在内的标准程序库
    • 条款55:让自己熟悉 Boost
Powered by GitBook
On this page

Was this helpful?

  1. 6.继承与面对对象设计

条款35:考虑 virtual 函数以外的其他选择

Consider alternatives to virtual functions.

在为解决问题寻找某个设计方法时,不妨考虑 virtual 函数的替代方案:

  • 使用 non-virtual interface(NVI);

  • 将 virtual 函数替换为 ”函数指针的成员变量”;

  • 以 tr1::function 成员变量替换 virtual 函数;

  • 将继承体系内的 virtual 函数替换为另一个继承体系内的 virtual 函数;

藉由 Non-Virtual Interface 手法实现 Template Method 模式

class GameCharacter {
public:
    int healthValue() const {
        ...
        int retVal = doHealthValue();
        ...
        return retVal;
    }
private:
    virtual int doHealthValue() const { ... }
};

通过 public non-virtual 成员函数间接调用 private virtual 函数的方法称为 non-virtual interface(NVI)手法,而这个 non-virtual 函数称为 virtual 函数的外覆器(wrapper)。外覆器可以保证在一个 virtual 函数被调用之前设定好适当场景,并在调用结束之后清理场景。

  • 事前工作:锁定互斥锁(locking a mutex)、制造运转日志记录项(log entry)、验证 class 约束条件、验证函数先决条件等等;

  • 事后工作:互斥器解除锁定(unlocking a mutex)、验证函数的事后条件、再次验证 class 约束条件等等;

”重新定义 virtual 函数“表示某些事”如何“被完成,”调用 virtual 函数“表示它”何时“被完成。NVI 手法允许 derived classes 重新定义 virtual 函数,从而赋予它们”如何实现机能“的控制能力,但 base class 保留诉说”函数何时被调用“的权利。

藉由 Function Pointers 实现 Strategy 模式

通过函数指针的方式实现运行时的多态:

class GameCharacter;
int defaultHealthCalc(const GameCharacter& gc);

class GameCharacter {
public:
    typedef int (*HealthCalcFunc)(const GameCharacter&);
    explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc): healthFunc(hcf) {}
    int healthVal() const {
        return healthFunc(*this);
    }
private:
    HealthCalcFunc healthFunc;
};

这种实现方式有两种弹性特性:

  • 同一个class 的不同实例可以有不同的计算函数;

  • 同一个实例的计算函数可随程序运行而改变;

藉由 tr1::function 完成 Strategy 模式

基于函数指针的做法比较死板,因为它:

  • 要求实现必须是一个函数,而不能是某种”像函数的东西(例如函数对象)“;

  • 要求函数不能是某个成员函数,不能是返回类型不同但可以被隐式转换的函数;

所以当客户需求更加弹性时,可以使用 tr1::function,它可持有任何可调用物(callable entity,即函数指针

函数对象或成员函数指针),只要其签名式兼容即可:

class GameCharacter;
int defaultHealthCalc(const GameCharacter& gc);

class GameCharacter {
public:
    typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc;
    explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc): healthFunc(hcf) {}
    int healthVal() const {
        return healthFunc(*this);
    }
private:
    HealthCalcFunc healthFunc;
};

其中 int (const GameCharacter&) 为目标签名式,即”接受一个 reference 指向 const GameCharacter 的参数,并返回 int“的调用物。而 tr1::function 相当于一个指向函数的泛化指针:

short calcHealth(const GameCharacter&);            // 计算函数,返回类型为 short

struct HealthCalculator {
    int operator()(const GameCharacter&) const {}  // 函数对象
};

class GameLevel {
public:
    float health(const GameCharacter&) const;      // 成员函数,返回为 float
};

GameCharacter g1(calcHealth);                      // 使用函数计算
GameCharacter g2(HealthCalculator());

GameLevel currentLevel;
GameCharacter g3(
    std::tr1::bind(&GameLevel::health, currentLevel, _1)
    );

GameLevel::health 宣称它接受一个参数,但实际上是两个,因为第一个参数是隐式参数 *this ,而 GameCharacter 要求的计算函数只接受单一参数。所以 currentLevel 即作为默认的 *this 值,而 tr1::bind 的作用指明这个关系。

古典的 Strategy 模式

传统的 Strategy 做法会将原 virtual 函数做成一个分离的继承体系中的 virtual 成员函数。

Previous条款34:区分接口继承和实现继承Next条款36:绝不重新定义继承而来的 non-virtual 函数

Last updated 6 years ago

Was this helpful?