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

时间:2023-01-15 20:39:48

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

我试图在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;
    }

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.

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

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

我得到的状态与IState不同,但是有一个运算符可以让我检查它们是否从同一个基类继承?也许重载运算符typeid可能会有所帮助,但这似乎很麻烦

4 个解决方案

#1


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

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

#2


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

执行此操作的正确方法是使用dynamic_cast,并检查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.

你使用的C风格的强制转换(避免那些BTW)实际上会执行safe_cast,它会在失败时抛出InvalidCastException,因此你必须明确地拼出dynamic_cast。

#3


Ok I managed to get it to work

好的,我设法让它工作

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

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

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 =)

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

#4


How about using the typeid operator? http://msdn.microsoft.com/en-us/library/fyf39xec(VS.80).aspx

使用typeid运算符怎么样? http://msdn.microsoft.com/en-us/library/fyf39xec(VS.80).aspx

#1


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

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

#2


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

执行此操作的正确方法是使用dynamic_cast,并检查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.

你使用的C风格的强制转换(避免那些BTW)实际上会执行safe_cast,它会在失败时抛出InvalidCastException,因此你必须明确地拼出dynamic_cast。

#3


Ok I managed to get it to work

好的,我设法让它工作

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

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

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 =)

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

#4


How about using the typeid operator? http://msdn.microsoft.com/en-us/library/fyf39xec(VS.80).aspx

使用typeid运算符怎么样? http://msdn.microsoft.com/en-us/library/fyf39xec(VS.80).aspx