如何检查两个对象是否继承自同一个基类?

发布于 2024-07-26 03:50:18 字数 748 浏览 2 评论 0原文

我正在尝试重写 C++ .NET 中的 Object::Equals,但遇到了一些困难

virtual bool IState::Equals(Object^ o) override{
        if (o->GetType() == IState::typeid){
            IState^ s = (IState^) o;
            if (s->type == this->type &&
                s->target_state == this->target_state &&
                s->current_state == this->current_state){
                return true;
            }
            else{
                return false;
            }
        }
        return false;
    }

如果 o 是且 IState,此代码可以正常工作。 但是,我从 IState 继承了 State。 如果我传递具有相同内容的 State,我希望 Equals 函数返回 true。

我知道 State 与 IState 不同,但是是否有一个运算符可以让我检查它们是否从同一个基类继承? 也许重载操作符 typeid可能有帮助,但这似乎很麻烦

I'm trying to override Object::Equals in C++ .NET but I'm running into some difficulties

virtual bool IState::Equals(Object^ o) override{
        if (o->GetType() == IState::typeid){
            IState^ s = (IState^) o;
            if (s->type == this->type &&
                s->target_state == this->target_state &&
                s->current_state == this->current_state){
                return true;
            }
            else{
                return false;
            }
        }
        return false;
    }

This code works fine if o is and IState. However, I inherit from IState with State. I'd like my Equals function to return true if I pass a State with the same content.

I get that State is not the same as IState, but is there an operator which will allow me to check if they inherit from the same base class?
Maybe overloading the operator typeid might help, but it seems like a lot of trouble for that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

装迷糊 2024-08-02 03:50:18

使用反射是一种选择吗? 如果是这样,请查看 Type::IsAssignableFrom() 方法,以及 Type::BaseType 财产。

Is using reflection an option? If so, take a look at the Type::IsAssignableFrom() method, and the Type::BaseType property.

岁月蹉跎了容颜 2024-08-02 03:50:18

正确的方法是使用dynamic_cast,并检查nullptr:

virtual bool IState::Equals(Object^ o) override {
    IState^ s = dynamic_cast<IState^>(o);
    if (s != nullptr) {
        ...
    }
    return false;
}

您使用的C风格强制转换(避免那些顺便说一句)实际上会执行safe_cast,这会在失败时抛出InvalidCastException,因此您必须显式地拼写出dynamic_cast。

The correct way to do this is to use dynamic_cast, and check for nullptr:

virtual bool IState::Equals(Object^ o) override {
    IState^ s = dynamic_cast<IState^>(o);
    if (s != nullptr) {
        ...
    }
    return false;
}

C-style cast (avoid those BTW) that you have used will actually do a safe_cast, which throws InvalidCastException on failure, so you have to spell out dynamic_cast explicitly.

冷心人i 2024-08-02 03:50:18

好的,我设法让它工作,

我在 ->GetType() 之后缺少 ->BaseType()

这是工作版本,

virtual bool IState::Equals(Object^ o) override{
        if (o->GetType()->BaseType() == IState::typeid){
                IState^ s = (IState^) o;
                if (s->type == this->type &&
                        s->target_state == this->target_state &&
                        s->current_state == this->current_state){
                        return true;
                }
                else{
                        return false;
                }
        }
        return false;
    }

谢谢大家的时间、支持和奉献 =)

Ok I managed to get it to work

I was missing the ->BaseType() after the ->GetType()

here is the working version

virtual bool IState::Equals(Object^ o) override{
        if (o->GetType()->BaseType() == IState::typeid){
                IState^ s = (IState^) o;
                if (s->type == this->type &&
                        s->target_state == this->target_state &&
                        s->current_state == this->current_state){
                        return true;
                }
                else{
                        return false;
                }
        }
        return false;
    }

thank you all for your time, support and devotion =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文