如何检查我的类的两个实例是否相等
How to check if two instances of my class are equal?
我创建了一个类,用户必须在其中输入他的地址。我的问题是如何实现一个成员函数来确定我的 Address 类的一个实例的数据成员是否等于另一个实例?
我假设你想知道两个地址是否相等。为此,您可以实现等于运算符 ( ==
),您可以通过检查所有地址字段是否相等来检查是否addressA == addressB
。例如:
bool operator==(const Address& rhs)
{
return (this->country == rhs.country) && (this->city== rhs.city) &&
(this->street_name == rhs.street_name) && (this->street_number == rhs.street_number) &&
(this->zip_code == rhs.zip_code) &&
(this->GPS_coordinates_latitude = rhs.GPS_coordinates_latitude) &&
(this->GPS_coordinates_longitude == rhs.GPS_coordinates_longitude);
}
相关文章:
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- C++两个源文件之间共享的枚举的静态实例
- 堆栈分配的类类型.为什么两个 ID 实例的地址相同?
- 比较两个 std::chrono::time_point 实例时出错
- 在两个类实例之间共享向量
- 编译时检查是否有两个具有相同模板参数的模板实例化
- 使用模板检查我的类的两个实例是否相等
- 如何将一个类的两个单独实例设置为彼此相等的 c++
- 如何在彼此中使用两个类的实例
- 尝试使用两个单独的 getline() 实例来填充两个单独的向量
- 没有成员作为唯一标识符的两个类实例的比较
- 随机洗牌似乎会产生相同的结果,但仅在前两个实例中产生相同的结果.我如何避免这种情况
- 使用Qt的两个"QLowEnergyController"实例连接到两个BLE设备
- 运算符*=来自两个实例(矩阵),结果不好
- 在抛出 std::bad_alloc 实例后终止调用.使用两个类,一个带有指向另一个的指针
- 如何超载分配运算符,该操作员总计两个实例变量
- 如何在c++中的两个函数中传递对象的同一实例
- 如何使用两个实例中的值进行计算
- 两个未初始化的实例之一
- 我可以在两个不同的类实例中使用boost asio套接字引用吗