2015-01-17 3 views
-2

나는 사용자가 그의 주소를 입력해야하는 수업을 만들었습니다. 내 질문은 내 Address 클래스 인스턴스의 데이터 멤버가 다른 인스턴스와 같은지 여부를 결정하는 멤버 함수를 구현하는 방법입니다.두 클래스 인스턴스가 같은지 확인하는 방법은 무엇입니까?

+1

방금 ​​OOP를 배우는 것 같습니다. 클래스의 완성을 위해 복사 생성자, 소멸자 및 오버로드 = 연산자가 있어야합니다. 또한 print() 멤버 함수를 사용하는 대신 << 연산자를 오버로드하여 std :: cout << address << std :: endl; 주소를 출력합니다. – TriHard8

답변

2

두 주소가 동일한 지 알고 싶다고 가정합니다. 이를 위해 등호 연산자 (==)를 구현할 수 있습니다. 여기서 모든 주소 필드가 동일한 지 확인하여 인지 확인할 수 있습니다. 예 :

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); 
    } 
+0

rhs에 'const'를 추가해야합니다. – TriHard8

+0

@ TriHard8 해결되었습니다. 감사! – Mustafa

관련 문제