如何检查我的类的两个实例是否相等

How to check if two instances of my class are equal?

本文关键字:实例 两个 是否 何检查 检查 我的      更新时间:2023-10-16

我创建了一个类,用户必须在其中输入他的地址。我的问题是如何实现一个成员函数来确定我的 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);
    }