noddy
noddy

Reputation: 1010

Equality operator for a structure in C++/CX`

I have implemented a value structure in C++/CX. For initializing a vector of the structure I need to implement an Equality operator which is necessary in the IndexOf() operator of a vector as present in Collection.h.

But I am unable to create one.Can some one help me with it?

Upvotes: 0

Views: 1197

Answers (1)

dcp
dcp

Reputation: 55464

Here's an example:

struct typePerson {
  string firstName;
  string lastName;

  typePerson(string firstName,string lastName) : firstName(firstName), lastName(lastName) {}  

  bool operator==(const typePerson& p2) const {
    const typePerson& p1=(*this);
    return p1.firstName == p2.firstName && p1.lastName == p2.lastName;
  }
};

Sample Usage:

int main() {
  typePerson p1("John","Doe");
  typePerson p2("Jane","Doe");
  typePerson p3("John","Doe");

  if (p1 == p2)
    cout<<"p1 equals p2"<<endl;
  else
    cout<<"p1 does not equal p2"<<endl;

  if (p1 == p3)
    cout<<"p1 equals p3"<<endl;
  else
    cout<<"p1 does not equal p3"<<endl;


  return 0;
}

---------- Capture Output ----------
p1 does not equal p2
p1 equals p3

> Terminated with exit code 0.

Upvotes: 2

Related Questions