Point p1{10,20};
Point p2{30,40};
//복잡한 멤버함수를 호출하는 것 대신
~~p1.ShowPosition();~~
~~p2.ShowPosition();~~
//기본 자료형 처럼 stream 출력 가능하도록 한다.
std::cout << p1 << std::endl; // [10,20] 출력
std::cout << p2 << std::endl; // [30,40] 출력
Point p3;
std::cin >>p3; //50,60 입력
객체를 cout, cin을 사용 할 수 있도록 해보자. 좀 더 편해지지 않을까?
#include <iostream>
class MyOstream
{
public:
void operator << (int val)// << 연산자 정의 해줘야함
{
printf("%d", val);
}
};
int main()
{
MyOstream mycout;
mycout << 10; //mycout.operator<<(10)로 변환 함
std::cout << 10;//즉, 이는 cout이 ostream의 객체였다는 것!
return 0;
}
우리가 MyOstream에 정의한 내용을 컴파일러가 변환을 하여 해석을 한다. 여기서 코드를 보면
’Mycout << 10;’ 과 ‘std::cout <<10;’이 같은 표현이라는 것을 알 수 있다.
이제 우리가 원래 해 보려던 객체를 스트림에 적용해 보자! 근데 한 가지 문제점이 있다.
int main()
{
std::cout << p1;//cout.operator<<(p1) or opeartor<<(cout,p1)으로 컴파일러가 해석
}
💡
위의 코드가 성립하도록 하기 위해서는
두 가지 방법으로 접근이 가능하다.
void operator<<(std::ostream& os, Point rhs)//전역 함수
{
}
int main()
{
Point p1{2,3};
std::cout << p1;
}
💡
여기서 잠깐! 위와 같은 형식으로 진행하지만 알아두어야 할 것이 있다.
cout은 복사가 방지 되어있기 때문에 반드시 참조자를 사용해야한다.
#include <iostream>
class Point
{
// 중략
//......
friend void operator<<(std::ostream& os,const Point& rhs)
{
os << "[" << rhs.xpos << "," << rhs.ypos << "]" << std::endl;
}
};
int main()
{
Point p1{ 2,3 };
std::cout << p1;//operator<<(cout,p1);으로 변환 됨
}
os는 cout을 매개변수로 받기 때문에 os가 cout의 역할을 하고 있다. 이제 어떻게 접근해야 하는지 알아봤으니 좀 더 자세하게 들어가 보자.
그럼 연속적으로 객체 2개를 넣을때는 어떻게해야 할까?
std::cout << p1 << p2;
friend ostream& operator<<(ostream& os, const Point& rhs)
{
os<<"[" << rhs.xpos << "," << rhs.ypos << "]";
return os;
}
int main()
{
Point p1{ 2,3 };
Point p2{ 3,4 };
std::cout << p1;//operator<<(cout,p1);으로 변환 됨
std::cout << p1 << p2 << std::endl;//불가능
}
불가능한 이유가 뭘까?
return 타입이 없을 때의 컴파일러의 해석을 단계별 접근해 보자!
(std::cout << p1) << p2 << std:: endl;
operator(cout, p1) << p2 << std::endl;
void << p2 << std::endl;
4 .
operator<<(void,p2) << std::endl; //불가능!
//정답) std::cout << p2 << std::endl;로 변경이 되어야 함
즉, return 값이 std::ostream& 이 되어야 한다. ( cout은 복사가 되지 않는다는 것을 명심해야 함)
friend istream& operator>>(istream& is , Point& rhs)
{
int x = 0 , y = 0;
is >> x >> y;
rhs = Point{x,y};
return is; //for chain insertion
}
int main()
{
int a;
std::cin >> a;
Point p3{ 0,0 };
std::cin >> p3; //operator>>(cin,p3);
}
rhs = Point{x,y};에서 살짝 헷갈린 부분이 있어서 간단하게 정리를 해보겠다.