자료구조_07.연산자 중복
2024. 4. 15. 15:04ㆍ자료구조와 게임 알고리즘
연산자 중복
C++ 표준 연산자를 객체에 대해서도 적용 할 수 있도록 정의하는것
string 클래스는 연산자를 중복사용하고 있음
int main() {
string s1 = "Rogue One: ";
string s2 = "A Star Wars Story";
string s3;
s3 = s1 + s2;
cout << "s1=" << s1 << endl;
cout << "s2=" << s2 << endl;
cout << "s1+s2=" << s3 << endl;
cout << "s1==s2 " << (s1 == s2) << endl;
}
중복할 수 없는 연산자
연산자 중복 정의
대입 연산자를 중복
class Box
{
private:
double length, width, height;
public:
// 멤버 초기화 리스트 작성해보자
void display() {
cout << "(" << length << ", " << width << ", " << height << ")" << endl;}
};
int main()
{
Box b1(30.0, 30.0, 60.0), b2;
b1.display();
b2 = b1;
b2.display();
}
Box& operator=(const Box& b2){
this->length = b2.length;
this->width = b2.width;
this->height = b2.height;
return *this;
}
대입연산자는 객체 자신을 반환해야 함
<- 대입 연산자는 연속으로 적용될 수 있기 때문
int main()
{
Box b1(30.0, 30.0, 60.0), b2, b3;
b3 = b2 = b1;
}
반환형이 참조자인 경우
'자료구조와 게임 알고리즘' 카테고리의 다른 글
자료구조_06. 클래스 (0) | 2024.04.15 |
---|---|
자료구조_08. 시간복잡도 (0) | 2024.04.15 |
자료구조_05.배열과 벡터 (0) | 2024.03.25 |
자료구조_04.함수 (0) | 2024.03.19 |
자료구조_03.제어구조 (1) | 2024.03.19 |