NVI
2016-04-16 12:03
423 查看
这是我写的简易版的, Effective C++, 3rd Edition, Item 35: 考虑可选的 virtual functions(虚拟函数)的替代方法
书上的程序
//类里面什么都没有(关于血量),这样只有通过一个指针调用类外函数,来返回当前血量,
//而且你也必须在类外定义掉血方式,这样一来,用过指针调用的函数,又有定义多个的必要了
//
//定义类外函数,然后把指针返回给类内指针,才是正道,把定义的函数指针,来创建函数是错误的想法
#include<iostream>//item 35 using namespace std; /*------------------NVI--------------------*/ class A{ public: void f() { f1(); cout << "f"; } private: //protected: virtual void f1() { cout << "A::f1"; } }; class B : public A { //public: virtual void f1() { cout << "B::f1"; } };
书上的程序
/*virtual*/ class GameChareter1 { public: virtual int healthVaule()const; }; /*strategy*/ class GameChareter { public: typedef int (*GameHealthCalcFcn) (GameChareter);// '*' zai qianmian GameChareter(GameHealthCalcFcn ht = defaultHealht): health(ht) { } int healthvaule()const { return health(*this);//this is a fucking fcn } private: GameHealthCalcFcn health; };
//类里面什么都没有(关于血量),这样只有通过一个指针调用类外函数,来返回当前血量,
//而且你也必须在类外定义掉血方式,这样一来,用过指针调用的函数,又有定义多个的必要了
//
//定义类外函数,然后把指针返回给类内指针,才是正道,把定义的函数指针,来创建函数是错误的想法
相关文章推荐
- hihoCoder 1015 KMP算法(kmp)
- Fragment页面的切换
- JAVA修改组件
- 【模拟】HDU4509湫湫系列故事——减肥记II
- Windows Docker的有趣事实 推荐
- Linux用户管理:addgroup、adduser、usermod
- android 调用远程service时报java.lang.SecurityException: Binder invocation to an incorrect interface异常
- position、opacity与z-index的组合
- 复利计算再升级
- 关于stm32定时器的理解
- SeimiAgent——基于QtWebkit的通用网页渲染代理服务
- 关于stm32定时器的理解
- Android 学习记录-零散知识点
- 11. Container With Most Water
- jQuery实现漂亮实用的商品图片tips提示框效果(无图片箭头+阴影)
- 什么是P问题、NP问题和NPC问题
- spring mvc中的@PathVariable
- CentOS安装软件<一> MYSQL
- c++(1)
- 单例模式的五种写法