c++ class 문법에 대하여 질문이 있습니다.
글쓴이: pdlkkldp / 작성시간: 금, 2019/07/12 - 10:18오전
#include
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(int _x = 0, int _y = 0) :x(_x), y(_y)
{
}
void print()
{
cout << x << ',' << y << endl;
}
const Point operator+(Point arg) const
{
Point pt;
pt.x = this->x + arg.x;
pt.y = this->y + arg.y;
return pt;
}
};
int main()
{
Point p1(2, 3), p2(5, 5);
Point p3;
p3 = p1 + p2;
p3.print();
p3 = p1.operator+(p2);
p3.print();
system("pause");
return 0;
}
클래스의 private 멤버변수에 접근, 초기화할 떄는 public의 함수, 생성자를 이용해야 한다고 알고 있는데,
Point operator+(Point arg)함수를 보면 포인터처럼 .과 ->를 이용해서 접근을 하는데 가능한 것인가요?
Forums:
Member function can access
Member function can access private members.
댓글 달기