条款31 让函数根据一个以上的对象类型来决定如何虚化
来源:互联网 发布:什么软件可以看港片 编辑:程序博客网 时间:2024/04/27 07:01
普通函数重载
#include<iostream>using namespace std;class SpaceShip;class SpaceStation;class Asteriod;class GameObject{ public:};class SpaceShip: public GameObject{ public:};class SpaceStation: public GameObject{ public:};class Asteriod: public GameObject{ public:};void collide(GameObject &ob1,GameObject &ob2){ cout<<"GameObject &ob1=>GameObject &ob2"<<endl;}void collide(GameObject& ob1,SpaceShip& ob2){ cout<<"GameObject &ob1=>SpaceShip &ob2"<<endl;}void collide(SpaceStation& ob1,Asteriod& ob2){ cout<<"SpaceStation &ob1=>Asteriod &ob2"<<endl;}void collide(SpaceShip &ob1,SpaceStation &ob2){ cout<<"SpaceShip &ob1=>SpaceStation &ob2"<<endl;}void TestCollide(GameObject &ob1,GameObject &ob2){ collide(ob1,ob2);}int main(){ SpaceShip sp; SpaceStation ss; TestCollide(sp,ss);//对象切片 cout<<"==============================="<<endl; collide(sp,ss);//调用重载函数 getchar(); return 0;}
结果:
条款31所述方法实现多态
#include<iostream>using namespace std;class SpaceShip;class SpaceStation;class Asteriod;class GameObject{ public: virtual void collide(GameObject& otherObject) = 0; virtual void collide(SpaceShip& otherObject) = 0; virtual void collide(SpaceStation& otherObject) = 0; virtual void collide(Asteriod& otherObject) = 0;};void GameObject::collide(GameObject& otherObject){ cout<<"Base::collide"<<endl;}class SpaceShip: public GameObject{ public: virtual void collide(GameObject& otherObject) { cout<<"SpaceShip::collide"<<endl; otherObject.collide(*this); } virtual void collide(SpaceShip& otherObject) { cout<<"SpaceShip::collide(SpaceShip& otherObject)"<<endl; } virtual void collide(SpaceStation& otherObject) { cout<<"SpaceShip::collide(SpaceStation& otherObject)"<<endl; } virtual void collide(Asteriod& otherObject) { cout<<"SpaceShip::collide(Asteriod& otherObject)"<<endl; }};class SpaceStation: public GameObject{ public: virtual void collide(GameObject& otherObject) { cout<<"SpaceStation::collide"<<endl; otherObject.collide(*this); } virtual void collide(SpaceShip& otherObject) { cout<<"SpaceStation::collide(SpaceShip& otherObject)"<<endl; } virtual void collide(SpaceStation& otherObject) { cout<<"SpaceStation::collide(SpaceStation& otherObject)"<<endl; } virtual void collide(Asteriod& otherObject) { cout<<"SpaceStation::collide(Asteriod& otherObject)"<<endl; }};class Asteriod: public GameObject{ public: virtual void collide(GameObject& otherObject) { cout<<"Asteriod::collide"<<endl; otherObject.collide(*this); } virtual void collide(SpaceShip& otherObject) { cout<<"Asteriod::collide(SpaceShip& otherObject)"<<endl; } virtual void collide(SpaceStation& otherObject) { cout<<"Asteriod::collide(SpaceStation& otherObject)"<<endl; } virtual void collide(Asteriod& otherObject) { cout<<"Asteriod::collide(Asteriod& otherObject)"<<endl; }};void TestCollide(GameObject &ob1,GameObject &ob2){ ob1.collide(ob2);}int main(){ SpaceShip oShip; SpaceStation oStation; Asteriod oAst; GameObject& g1=oShip; GameObject& g2=oStation; GameObject& g3=oAst;TestCollide(g1,g1);cout<<"==============================="<<endl;TestCollide(g1,g2);cout<<"==============================="<<endl;TestCollide(g1,g3);cout<<"==============================="<<endl;TestCollide(g2,g3); getchar(); return 0;}
结果:
- 条款31 让函数根据一个以上的对象类型来决定如何虚化
- 条款三十一:让函数根据一个以上的对象类型来决定如何虚化
- item31让函数根据一个以上的对象类型来决定如何虚化
- 技术(7)——让函数根据一个以上的对象类型来决定如何虚化
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(1)
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(2)
- More Effective C++ Item M31:让函数根据一个以上的对象来决定怎么虚拟
- 如何根据一个值来动态让select下的option选中
- 让UILabel根据内容字体的多少,来决定自己的高度
- js 让函数立马变成一个 类 来实例化一个对象
- 【JSP页面】如何根据Select的选项来决定页面input的显示隐藏
- 右边的类对象决定查看的虚函数表..左边的类型调用非函数表
- 根据宽度来决定显示的字符串长度(C#)
- 如何知道一个python对象的类型?
- 在宏定义中如何使用一个外部文件的的值来决定宏定义
- 虚函数,有的语言里也叫动态函数(DYNAMIC,相对于VIRTUAL),这里的虚,不是没有,而是说,调用对象的类型决定具体的函数。
- 用户决定消息类型的函数
- 如何根据一个字符串去查找同名的对象?
- 25个必须记住的SSH命令
- Verilog HDL Behavioral Modeling Part-V
- 有效解决Android加载大图片时内存溢出的问题
- 全面解析E-MAIL编码
- sock5代理软件—SS5的安装配置
- 条款31 让函数根据一个以上的对象类型来决定如何虚化
- amcharts_flash_1.6(绝版了)破解说明
- 集合框架 Queue---ArrayBlockingQueue
- VC++的心得
- 把串口读写重定向到tcp
- 建造者模式【Builder Pattern】
- [Java]图解Perfect shuffle[Jai04]
- 应用程序的名字分两行显示
- MFC 进度条