0

我在 C++/CX 中实现了一个值结构。为了初始化结构的向量,我需要实现一个 Equality 运算符,这在 Collection.h 中存在的向量的 IndexOf() 运算符中是必需的。

但我无法创建一个。有人可以帮我吗?

4

1 回答 1

2

这是一个例子:

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;
  }
};

样品用法:

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.
于 2012-07-06T12:14:01.193 回答