Overloaded == and comparing with nullptr?

I am writing a C ++ / CLI wrapper from the Cical Native Static Library.

I do not have much experience with C ++ / CLI or C ++. I followed the best practices that I read on the Internet for creating C ++ / CLI wrappers. My shell has a pointer to a C ++ class. And my C ++ is overloaded with the == operator.

I try to overload it also in my shell and use the C ++ class implementation, but I get errors when the shell is null.

I was looking for how to find out if my descriptor is null, and I found that you need to compare it with nullptr.

I have this method in C ++ / CLI

       MyOtherClass const* MyClass::MyMethod(MyWrapper^ instance)   
    {
        if(instance == nullptr)
        {
           return NULL;
        }

        return instance->Property;      
    }

A string if (instance == nullptr) calls my overloaded implementation of the == operator .

      static bool operator==(MyWrapper^ a, MyWrapper^ b)
    {
        return a->InternalInstance == b->InternalInstance; //Here System.AccessViolationException Exception
    }

, a null, System.AccessViolationException.

nullptr a b, .

static bool operator==(MyWrapper^ a, MyWrapper^ b)
{
    if(a == nullptr && b == nullptr) //Qaru here because a == nullptr calls this method again.
        return true;
    if((a == nullptr && b != nullptr) || (a != nullptr && b == nullptr))
        return false;
    return a->InternalInstance == b->InternalInstance;
}

== ++ - null?

+3
2

Object::ReferenceEquals, null.

static bool operator==(MyWrapper^ a, MyWrapper^ b)
{
    if(Object::ReferenceEquals(a, nullptr) && Object::ReferenceEquals(b, nullptr))
        return true;

    if(Object::ReferenceEquals(a, nullptr) || Object::ReferenceEquals(b, nullptr))
        return false;

    return a->InternalInstance == b->InternalInstance;
}
+7

, o == ++/CLI, , , ++, , == . native ++ == operator :

bool operator==(const X& lhs, const X& rhs){ /* do actual comparison */ }

, == native ++, ?

== , , , operator ==. Equals ==

: Equals() Operator ==. #, , ++/CLI.

+2

All Articles