gpt4 book ai didi

c++ - 如何判断两个多态对象是否相等?

转载 作者:太空狗 更新时间:2023-10-29 21:20:34 26 4
gpt4 key购买 nike

<分区>

如何判断两个多态对象是否相等?例如

struct A {};

struct A1 : public A
{
int i;
bool operator==(A1 const& other) const { return i == other.i; }
};

struct A2 : public A
{
float f;
bool operator==(A2 const& other) const { return f == other.f; }
};

bool isEqual(A const& x, A const& y)
{
if (typeid(x) != typeid(y))
return false;

... // Then how to compare when they have same type. Any way to cast them
// to their dynamic types by typeid or others
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com