烦请各位看下这个抽象有没有更好的实现?
需求是这样的,需要定义一个抽象的PeerId,PeerId的具体实现不确定,从上层来看,PeerId只要能够实现比较就OK了,因此,抽象PeerId的定义如下:
struct PeerId
{
virtual bool operator==(const PeerId& p) = 0;
};
class XXXPeerId : public PeerId
{
public:
XXXPeerId(int i) : a(i) {}
bool operator==(const PeerId& p)
{
try
{
const XXXPeerId& xp = dynamic_cast<const XXXPeerId&>(p);
return a == p.a;
}
catch (...)
{
return false;
}
}
private:
int a;
};