NVI和函数指针代替虚函数
来源:互联网 发布:电机控制算法书籍 编辑:程序博客网 时间:2024/05/16 19:32
NVI(Non-Virtual Interface)
#include<iostream>using namespace std;/////////////////////class GameCharacter{public:int GetHealthValue() const{int retVal = doHealthValue();return retVal;}GameCharacter(int healthValue = 222){this->healthValue = healthValue;}private:int healthValue;virtual int doHealthValue() const{return healthValue;}};//////////////////////////////////////////void main(){GameCharacter* gamePeople = NULL;gamePeople = new GameCharacter();cout << gamePeople->GetHealthValue() << endl;delete gamePeople;}/*222*/
函数指针:
#include<iostream>using namespace std;class GameCharacter;int defaultHealthCalc( GameCharacter& gc);class GameCharacter{public: typedef int (* HealthCalcFunc)(GameCharacter&);explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc):healthFunc(hcf){healthValue = 666;} int GetHealthValue() {return healthFunc(*this);}void SetHealthValue(int healthValue = 222){this->healthValue = healthValue;}int GetHealthValue2(){return healthValue;}private:HealthCalcFunc healthFunc;int healthValue;};int defaultHealthCalc( GameCharacter& gc){gc.SetHealthValue(444);return gc.GetHealthValue2();}void main(){GameCharacter gamePeople;cout << gamePeople.GetHealthValue() << endl;}/*444*/
- NVI和函数指针代替虚函数
- 使用函数指针来代替switch语句
- 用函数指针代替switch-case
- 函数指针和函数
- 指针函数和函数指针
- 函数指针和指针函数
- 指针函数和函数指针
- 指针函数和函数指针
- 函数指针和指针函数
- 函数指针和指针函数
- 函数指针和指针函数
- 指针函数和函数指针
- 函数指针和指针函数
- 指针函数和函数指针
- 函数指针和指针函数
- 指针函数和函数指针
- 指针函数和函数指针
- 指针函数和函数指针
- 解决12306.cn网站验证码获取提示“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系“的问题
- iOS之第三方FMDB框架操作数据库
- J2SE 必知必会(二)
- What is a DesignPattern? 什么是设计模式
- 场效应管导通条件
- NVI和函数指针代替虚函数
- Teach Yourself Programming in Ten Years--用十年来学编程
- iScroll滚动衔接浏览器原生滚动
- ubuntu(xubuntu) 安装LAMP 环境
- J2SE 必知必会(三)
- GDB调试多线程程序
- 从重视研发到建立高效的研发管理体系
- 编辑器截图
- 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器