Saher Ahwal
Saher Ahwal

Reputation: 9237

Equality with an empty (null-like) struct

The answer to the question here addresses initializing a null-reset or zero struct.

How can I check equality though?

say my struct x is defined as follows:

struct MyStruct {
int a;
int b;

};

and the empty struct :

static const struct MyStruct EmptyStruct;

how do I check equality inside a function that takes a reference to a struct of type x?

void myFunction (... , MyStruct &x, ...){
//some code


if (x != EmptyStruct){  // this doesn't work (see error below)


}


//some code
}

The error I get when I try the above:

no match for 'operator!=' in 'x != EmptyStruct'

EDIT: to make it more clear I understand the error message in terms of overloading the != operator for the struct but since an EmptyStruct is a special kind, how can I deal with that?

I guess the point is that a struct of my type with a = 0 and b = 0 is not the same as the EmptyStruct which should represent null-like struct.

Upvotes: 0

Views: 938

Answers (5)

Jerry Coffin
Jerry Coffin

Reputation: 490148

You really need to make up your mind whether your MyStruct is a value type or an identity type.

If it's a value type, then you want to define some part of its value as saying that it's empty:

struct MyStruct { 
    int x;
    int y;

    bool empty;
};

If it's an identity type, then you want to check for identity:

void myFunction(MyStruct &m) {
    if (&m == &EmptyStruct)
        // reference to EmptyStruct. Act accordingly.
}

If you need a singular value like your EmptyStruct, you might want to consider passing a (smart) pointer, and comparing to nullptr to determine whether you've received a singular value.

As a general rule, I'd avoid this type of design in general. Identity types are somewhat foreign to most C++ programmers, and singular values create special cases that you're generally better off without.

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254471

It sounds like you want to test whether or not x refers to the same object as EmptyStruct, in which case you want to compare addresses:

if (&x != &EmptyStruct)

although this is a rather odd thing to do; it usually makes more sense to use a pointer, with a well-defined null value to represent "no object", if you want a nullable reference-like type.

== and != are used to compare values, not object identities, and have to be overloaded if you want to define them for class types.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477070

Since you are taking the object by reference, I assume you want to test if the user actually passed the global EmptyStruct object as a function argument. This can be achieved easily by comparing addresses:

void foo(MyStruct & x)
{
    if (&x == &EmptyStruct) { /* ... */ }
}

(This assumes that MyStruct does not overload operator&.)

Upvotes: 0

Paweł Stawarz
Paweł Stawarz

Reputation: 4012

Simply overload the operator== (or operator!=). Or (since by default all members of a struct are public), check all elements inside the structures.

Since your talking something about that your struct is "empty" - there's no such thing. When you create an object, the memory for it should be allocated automatically by the constructor. If you want to check if the passed struct is the static EmptyStruct, check the addresses of both.

Upvotes: 0

Floris
Floris

Reputation: 46375

You need to overload the equality operator, testing every element of the structure.

bool MyClass::operator==(const MyClass &other) const {
    ...  // Compare the values, and return a bool result.
  }

Upvotes: 1

Related Questions