0

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.

Community
  • 1
  • 1
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
  • 2
    You need to define operator != for type `MyStruct`. This is what the error message is telling you, it cannot find it. – Borgleader Feb 04 '14 at 04:00
  • I get that error message, I am asking for dealing with the empty struct – Saher Ahwal Feb 04 '14 at 04:02
  • This question should not be tagged both `c` and `c++`. The answer is radically different depending on which language you're using. Your error message suggests you're using C++, but some beginners are inadvertently compiling code meant to be C using a C++ compiler, so it's not clear. – R.. GitHub STOP HELPING ICE Feb 04 '14 at 04:02
  • The example you link is C, not C++ - and since you are talking about a `struct` and not a `class` I am beginning to get very confused. Could you please post a small sample program that defines your program and `EmptyStruct` - and that compiles up to the `==` line? – Floris Feb 04 '14 at 04:07
  • The first question would be whether you care about *equality* or *identity*. If you care about equality (multiple different objects can be *null* and compare equal), then you will need to find either a value that is out of range to represent *null* or else change and use a different approach (a pointer can be *null*, a reference cannot; an `optional` can be *null*…) – David Rodríguez - dribeas Feb 04 '14 at 04:32

5 Answers5

2

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

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.
  }
Floris
  • 45,857
  • 6
  • 70
  • 122
0

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.

Community
  • 1
  • 1
Paweł Stawarz
  • 3,952
  • 2
  • 17
  • 26
0

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&.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644