[C++] 19. μ°μ°μ μ€λ²λ‘λ©(Operator Overloading)
μ°μ°μ μ€λ²λ‘λ©(Operator Overloading)
μ°μ°μ μ€λ²λ‘λ©μ μ¬μ©μ μ μ λ°μ΄ν° νμ
μ λν΄ κΈ°μ‘΄μ μ°μ°μλ₯Ό μλ‘ μ μνμ¬ μ¬μ©ν μ μκ² νλ κΈ°λ₯μ
λλ€. μ¦ μ°μ°μ(+, -, /, *, ++, --, =, ==, <, >, <=, >= λ±)λ₯Ό λ΄λ§μλλ‘ μ¬μ μνλ κ²μ
λλ€. μ΄λ₯Ό ν΅ν΄ μ¬μ©μκ° μ μν ν΄λμ€λ ꡬ쑰체μ λν΄ μ°μ μ°μ°, λΉκ΅ μ°μ° λ±μ μ°μ°μλ₯Ό μ¬μ©ν μ μλλ‘ ν μ μμ΅λλ€. μ°μ°μ μ€λ²λ‘λ©μ C++μ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ ν΅μ¬μ μΈ νΉμ§ μ€ νλλ‘, μ½λ κ°λ
μ±μ λμ΄κ³ μ§κ΄μ μΈ μΈν°νμ΄μ€λ₯Ό μ 곡νλ λ°μ μ μ©ν©λλ€.
μ°μ°μ μ€λ²λ‘λ©μ λ€μκ³Ό κ°μ νμμΌλ‘ ꡬνλ©λλ€.
return_type operator μ°μ°μ(λ§€κ°λ³μ) {
// μ°μ°μλ₯Ό μ μνλ μ½λ
}
μ¬κΈ°μ `return_type`μ μ°μ° κ²°κ³Όμ λ°ν νμ
μ΄λ©°, `μ°μ°μ`λ μ€λ²λ‘λ©νκ³ μ νλ μ°μ°μλ₯Ό λνλ
λλ€. λ§€κ°λ³μλ ν΄λΉ μ°μ°μμ νΌμ°μ°μλ€μ λνλ
λλ€.
μ°μ°μ μ€λ²λ‘λ©μ μμλ₯Ό μμ보μμ£ .
μ΄ν μ°μ°μ μ€λ²λ‘λ©
// + μ°μ°μ μ€λ²λ‘λ© μμ (λ²‘ν° λ§μ
)
class Vector {
public:
int x, y;
Vector() : x(0), y(0) {}
Vector(int _x, int _y) : x(_x), y(_y) {}
Vector operator+(const Vector& other) const {
return Vector(x + other.x, y + other.y);
}
};
int main() {
Vector v1(1, 2);
Vector v2(3, 4);
Vector result = v1 + v2; // μ¬μ©μ μ μλ + μ°μ°μλ₯Ό ν΅ν΄ λ²‘ν° λ§μ
μν
return 0;
}
λ¨ν μ°μ°μ μ€λ²λ‘λ©
// - μ°μ°μ μ€λ²λ‘λ© μμ (λ²‘ν° λΆνΈ λ°μ )
class Vector {
public:
int x, y;
Vector() : x(0), y(0) {}
Vector(int _x, int _y) : x(_x), y(_y) {}
Vector operator-() const {
return Vector(-x, -y);
}
};
int main() {
Vector v1(1, 2);
Vector result = -v1; // μ¬μ©μ μ μλ - μ°μ°μλ₯Ό ν΅ν΄ λ²‘ν° λΆνΈ λ°μ μν
return 0;
}
λΉκ΅ μ°μ°μ μ€λ²λ‘λ©
// == μ°μ°μ μ€λ²λ‘λ© μμ (λ²‘ν° λΉκ΅)
class Vector {
public:
int x, y;
Vector() : x(0), y(0) {}
Vector(int _x, int _y) : x(_x), y(_y) {}
bool operator==(const Vector& other) const {
return x == other.x && y == other.y;
}
};
int main() {
Vector v1(1, 2);
Vector v2(1, 2);
if (v1 == v2) {
// μ¬μ©μ μ μλ == μ°μ°μλ₯Ό ν΅ν΄ λ²‘ν° λΉκ΅ μν
}
return 0;
}
μ λ ¬μ νμν λΉκ΅μ°μ°μ μ€λ²λ‘λ©
C++μ std::mapμ κΈ°λ³Έμ μΌλ‘ keyλ₯Ό μ€λ¦μ°¨μμΌλ‘ μ λ ¬ν©λλ€. λ°λΌμ keyκ° μ¬μ©μ μ μ ν΄λμ€μΈ κ²½μ°, std::mapμμ keyλ₯Ό μ λλ‘ μ λ ¬νκΈ° μν΄ μ°μ°μ μ€λ²λ‘λ©μ΄ νμν©λλ€. std::mapμ keyλ₯Ό λΉκ΅νκΈ° μν΄ λΉκ΅ μ°μ°μλ₯Ό μ¬μ©νλ©°, λ°λΌμ ν΄λΉ λΉκ΅ μ°μ°μλ₯Ό μ¬μ©μ μ μ ν΄λμ€μ μ€λ²λ‘λ©ν΄μΌ ν©λλ€.
#include <iostream>
#include <map>
#include <string>
class Person {
public:
std::string name;
int age;
// μμ±μ
Person(const std::string& _name, int _age) : name(_name), age(_age) {}
// λΉκ΅ μ°μ°μ μ€λ²λ‘λ© (<)
bool operator<(const Person& other) const {
// μ΄λ¦(name)μ κΈ°μ€μΌλ‘ μ λ ¬
return name < other.name;
}
// λΉκ΅ μ°μ°μ μ€λ²λ‘λ© (==)
bool operator==(const Person& other) const {
// μ΄λ¦(name)κ³Ό λμ΄(age)κ° λͺ¨λ κ°μ λ trueλ₯Ό λ°ν
return name == other.name && age == other.age;
}
};
int main() {
std::map<Person, int> personMap;
Person person1("Alice", 30);
Person person2("Bob", 25);
Person person3("Alice", 35);
// std::mapμ μ½μ
personMap[person1] = 1;
personMap[person2] = 2;
personMap[person3] = 3;
// μΆλ ₯ κ²°κ³Ό: (Alice, 30): 1 (Alice, 35): 3 (Bob, 25): 2
for (const auto& entry : personMap) {
std::cout << "(" << entry.first.name << ", " << entry.first.age << "): " << entry.second << std::endl;
}
return 0;
}
μμ μμμμ Person ν΄λμ€λ μ΄λ¦(name)κ³Ό λμ΄(age)λΌλ λ κ°μ§ λ©€λ² λ³μλ₯Ό κ°μ§κ³ μμ΅λλ€. Person ν΄λμ€μ λΉκ΅ μ°μ°μ `<`μ `==`λ₯Ό μ μνμ¬ μ΄λ¦(name)μ κΈ°μ€μΌλ‘ μ€λ¦μ°¨μμΌλ‘ μ λ ¬λλλ‘ νκ³ , `==` μ°μ°μλ₯Ό ν΅ν΄ λ κ°μ Person κ°μ²΄κ° κ°μμ§ λΉκ΅ν©λλ€.
std::mapμ΄ keyλ₯Ό μ λ ¬νκΈ° μν΄μλ μ΄λ¬ν λΉκ΅ μ°μ°μ μ€λ²λ‘λ©μ΄ νμνλ©°, νμμ λ°λΌ keyλ₯Ό λΉκ΅ν λ λ€λ₯Έ κΈ°μ€μΌλ‘ μ λ ¬νλλ‘ λΉκ΅ μ°μ°μλ₯Ό μμ ν μ μμ΅λλ€.
μ 리
μ°μ°μ μ€λ²λ‘λ©μ C++μμ κ°λ ₯ν κΈ°λ₯μ΄μ§λ§, μ€μ©ν κ²½μ° μ½λμ κ°λ μ±μ μ νμν€κ³ νΌλμ μΌκΈ°ν μ μμ΅λλ€. λ€λ₯Έ λλΆλΆμ μΈμ΄μ μ°μ°μ μ€λ²λ‘λ©μ΄ μλ μ΄μ μ λλ€.
λ°λΌμ μ¬μ©μ μ μ ν΄λμ€μ μ°μ°μ μ€λ²λ‘λ©μ μΆκ°ν λλ λͺ ννκ³ μλ―Έ μλ λμμ μ 곡νλ κ²μ΄ μ€μν©λλ€. λν μΌλΆ μ°μ°μμ λν΄ μ€λ²λ‘λ©μ μ§μνμ§ μκ±°λ, νΉμ κ·μΉμ λ°λ₯΄λ κ²½μ°λ μμΌλ―λ‘ μ΄λ¬ν μ¬νμ λ¬Έμλ μ£ΌμμΌλ‘ λͺ μνλ κ²μ΄ μ’μ΅λλ€.