* 간단하다고 하면 간단한 예제 입니다. C++ 에서는 보통 vector 같은 클래스를 이용해서 동적으로 생성되는 객체를 보관하고 사용합니다. 그러나 간혹 C언어 방식의 데이터 처리가 필요할 때가 있습니다. 그런 경우로 예제를 보이겠습니다.



pointer.txt


 Pointer.cpp
 

#include <iostream>
using namespace std;

class Point {
protected:
int m_x, m_y;

public:
void Print() const;
void SetXY(int x, int y);
bool IsEqual(const Point& p) const;
Point(int x=0, int y=0);
};

void Point::Print() const {
cout << "(" << m_x << ", " << m_y << ")";
}

void Point::SetXY(int x, int y) {
m_x = x;
m_y = y;
}

bool Point::IsEqual(const Point& p) const {
return (m_x == p.m_x && m_y == p.m_y);
}

Point::Point(int x, int y) {
m_x = x;
m_y = y;
}

int main() {
Point *arr[3] = { new Point(10, 10), new Point(20, 20), new Point(30, 30)
};

for(int i=0; i<3; i++) {
cout << "arr[" << i << "] = ";
arr[i]->Print();
cout << endl;
}

for(int i=0; i<3; i++) {
delete arr[i];
}

return 0;
}



 - 실행 콘솔창
 

 

 
 


posted by 쪼재