'2015/09/02'에 해당되는 글 2건

  1. 2015.09.02 -> , * 연산자 오버로딩
  2. 2015.09.02 간단한 오버라이딩

#include <iostream>

using namespace std;


class Point {

int x;

int y;

public:

Point(int _x = 0, int _y = 0) :x(_x), y(_y) {}

void Print() const { cout << x << ", " << endl; }

};


class PointPtr

{

Point *ptr;

public:

PointPtr(Point *p) :ptr(p) {}

~PointPtr() { delete ptr; }


Point* operator->() const

{

return ptr;

}

Point& operator*() const

{

return *ptr;

}

};


int main()

{

Point* p1 = new Point(2, 3);

PointPtr p2 = new Point(5, 5);


p1->Print();

p2->Print();


return 0;

}


p1은 일반 포인터, p2가 스마트 포인터

'Programing > C++' 카테고리의 다른 글

멤버 함수 포인터 선언  (0) 2015.09.04
형변환 연산자 오버로딩  (0) 2015.09.04
간단한 오버라이딩  (0) 2015.09.02
구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
Posted by kimmayer

#include <iostream>

using namespace std;


class Point {

int x, y;


public:

Point(int _x = 0, int _y = 0) :x(_x), y(_y) {}

void Print() const {

cout << x << ", " << y << endl;

}

const Point operator+(const Point &arg) const

{

Point pt;

pt.x = this->x + arg.x;

pt.y = this->y + arg.y;


return pt;

}


const Point& operator++()

{

++x;

++y;


return *this;

}

const Point operator++(int)

{

Point pt(x, y);

++x;

++y;

return pt;

}

};


int main()

{

Point pt1(2, 3);

Point pt2(3, 5);

++pt2;

Point pt3;

pt3 = pt1 + pt2;


pt3.Print();

pt2.Print();

return 0;

}

'Programing > C++' 카테고리의 다른 글

형변환 연산자 오버로딩  (0) 2015.09.04
-> , * 연산자 오버로딩  (0) 2015.09.02
구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
Posted by kimmayer
이전버튼 1 이전버튼

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2015.9
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함