条款31 让函数根据一个以上的对象类型来决定如何虚化
2012-11-27 15:20
405 查看
普通函数重载
#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;
}
结果:
#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;
}
结果:
相关文章推荐
- 条款三十一:让函数根据一个以上的对象类型来决定如何虚化
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(2)
- item31让函数根据一个以上的对象类型来决定如何虚化
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(2)
- 让函数根据一个以上的对象类型来决定如何虚化
- 【M31】让函数根据一个以上的对象类型来决定如何虚化
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(1)
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(1)
- 技术(7)——让函数根据一个以上的对象类型来决定如何虚化
- More Effective C++ Item M31:让函数根据一个以上的对象来决定怎么虚拟
- 【C++】让函数根据一个以上的对象来决定怎么虚拟
- 一个函数锁住相同类型的多个对象造成的死锁
- 多态继承情况下,有一个基类指针指向派生类对象,如何用它调用基类的虚函数?
- 如何得知一个__ComObject对象的实际类型
- 如何使用JRtpLib3.5.0库中的函数和类对象和让JRTPLIB 发送/接收数据时使用同一个端口收藏
- 条款31: 千万不要返回局部对象的引用,也不要返回函数内部用new初始化的指针的引用
- Effective C++ 条款31: 千万不要返回局部对象的引用,也不要返回函数内部用new初始化的指针的引用
- .NET : 如何动态根据一个业务实体类型创建XSD架构文件
- 一个对象类型的数据如何转化为String类型
- C++面向对象程序设计中如何判断一个对象的类类型