条款35:考虑virtual函数以外的其他选择
有一部分人总是主张virtual函数几乎总应该是private:例如下面这个例子,例子时候游戏,游戏里面的任务都拥有健康值这一属性:
class GameCharacter{
public:
int healthValue()const{
...
int retVal = doHealthValue();
...
return retVal;
}
private:
virtual int doHealthValue() const
{
}//上面的这个non-virtual函数实际上是virtual函数的一个外覆器
};
class GameCharacter;
int defaultHealthCalc(const GameCharacter & gc);
class GameCharacter{
public:
typedef std::function<int (const GameCharacter & )> HealCalcFunc
explicit GameCharacter(HealCalcFunc hcf = defaultHealthCalc)
:healthFunc(hcf){}
int healthValue() const
{return healthFunc(* this); }
...
private:
HealCalcFunc healthFunc;
};
实际上,这个typedef带来的弹性是很大的:
short clacHealth(const GameCharacter &);
//或者是一个函数对象
struct HealCalculater{
public:
float operator()(const GameCharacters & gc) const ;
};
//或者是一个成员函数。
class GameLevel{
public:
float Health(const GameCharacters)const;
};
GameCharacters character1(clacHealth);
GameCharacters character2(HealCalculater());
GameLevel levelObj;
GameCharacters character3(std::bind(&GameLevel::health, levelObj, _1));//不记得这样写对不对,待查
class HealCalculater;
class GameCharacter;
int defaultHealthCalc(const GameCharacter & gc);
class GameCharacter{
public:
typedef std::function<int (const GameCharacter & )> HealCalcFunc
explicit GameCharacter(HealCalcFunc hcf = defaultHealthCalc)
:healthFunc(hcf){}
int healthValue() const
{return healthFunc(* this); }
...
private:
HealCalcFunc * phealthFunc;
};
HealCalculater{
public :
virtual int calc(const GameCharacter & gc)const
{} //也可以只声明一个接口
}
条款35:考虑virtual函数以外的其他选择的更多相关文章
- Effective C++ -----条款35:考虑virtual函数以外的其他选择
virtual函数的替代方案包括NVI手法及Strategy设计模式的多种手法.NVI手法自身是一个特殊形式的Template Method设计模式. 将机能从成员函数移到class外部函数,带来的一 ...
- Effective C++:条款35:考虑virtual函数以外的其它选择
游戏中的人物伤害值计算问题. (一)方法(1):一般来讲能够使用虚函数的方法: class GameCharacter { public: virtual int healthValue() cons ...
- 条款35:考虑virtual函数以外的其他选择(Consider alternative to virtual functions)
NOTE: 1.virtual 函数的替代方案包括NVI手法及Strategy设计模式的多种形式.NVI手法自身是一个特殊形式的Template Method设计模式. 2.将机能从成员函数移到外部函 ...
- 读书笔记_Effective_C++_条款三十五:考虑virtual函数以外的其他选择
举书上的例子,考虑一个virtual函数的应用实例: class GameCharacter { private: int BaseHealth; public: virtual int GetHea ...
- 考虑virtual函数以外的其它选择
详情见<Effective C++>item35 1.使用non-virtual interface(NVI)手法,这是Template Method设计模式的一种特殊形式. 它以publ ...
- [EffectiveC++]item35:考虑virtual函数以外的其他选择
本质上是说了: Template Pattern & Strategy Pattern 详细见<C++设计模式 23种设计模式.pdf 55页> 宁可要组合 不要继承. ——— ...
- EC读书笔记系列之16:条款35、36、37、38、39、40
条款35 考虑virtual函数以外的其他选择 记住: ★virtual函数的替代方案包括NVI手法及Strategy模式的多种形式.NVI手法自身是一个特殊形式的Template Method模式 ...
- Effective C++ -----条款09:绝不在构造和析构过程中调用virtual函数
在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层).
- 条款9:不要在构造和析构过程中调用virtual函数
如下是一个股票交易的例子: class Transaction // 交易的基类 { public: Transaction(); ; // 用于记录交易日志 }; Transaction::Tran ...
随机推荐
- vue+django前后端分析解决csrf token问题
vue-resource post数据 参考:https://www.cnblogs.com/linxizhifeng/p/8995077.html 阅读django CsrfViewMiddlewa ...
- Simple Tips for Collection in Python
I believe that the following Python code is really not hard to understand. But I think we should use ...
- C#多线程基础,适合新手了解
一.创建线程 在整个系列文章中,我们主要使用Visual Studio 2015作为线程编程的主要工具.在C#语言中创建.使用线程只需要按以下步骤编写即可: 1.启动Visual Studio 201 ...
- Java中finalize()用法
Java中finalize() 垃圾回收器要回收对象的时候,首先要调用这个类的finalize方法(你可以 写程序验证这个结论),一般的纯Java编写的Class不需要重新覆盖这个方法,因为Obj ...
- c# 虚方法(virtual)与 多态(Polymorphism)
using System; using System.Collections.Generic; using System.Linq; using System.Text; //虚方法(virtual) ...
- pdo封装2
<?php //添加了一个 _createSql 方法,负责创建所有sql class Db{ static private $ins; private $pdo; private $table ...
- HTML学习笔记(上)
1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...
- 20145239杜文超《网络对抗》- Web基础
20145239杜文超<网络对抗>- Web基础 基础问题回答 1.什么是表单? 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等)输 ...
- MySQL数据库基本操作(一)
进入mysql 本地连接: mysql -u用户名 -p 输入密码 qwe123 mysql -uroot -pqwe123 sudo apt-get install mysql-server # p ...
- OC_链表实现队列
@interface Node : NSObject @property(nonatomic,strong)NSString *value; @property(nonatomic,strong)No ...