ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 객체 배열, 객체포인터배열, this, friend
    플밍/C++ (overview) 2012. 1. 3. 22:58
    2006/08/01 13:35



    class Point{
          int x;
          int y;
    public:
          Point(){
                cout<<"Point() called!" << endl;
                x=y=0;
          }
    };

     

    int main(){

          Point arr[3];
          cout<<"arr[3] created." << endl; 
          return 0;
    }

     

    위의 프로그램 결과는

    --------------

    Point() called!

    Point() called!

    Point() called!

    arr[3] created.

    --------------

     

    이것은 객체 배열이다. 객체배열이 선언되자마자 각각 cell에 있는 객체들의 생성자가 호출된다.

     

    ===========================================================

    class Point{
           int x;
           int y;
    public:
           Point(){
                  cout<<"Point() called!" << endl;
                  x=y=0;
           }
           Point(int _x, int _y){
                  x = _x;
                  y = _y;
           }
    };

     

    int main(){
           Point* arr[3]; 
           cout<<"arr[3] created." << endl; 
           return 0;
    }


     

    결과는 하나의 warning 메세지를 보여준다. arr이 reference된곳이 없다는 메세지이다.

    그리고, console 창에는 다음과 같은 결과가 나온다.

     

    --------------

    arr[3] created.

    --------------

     

    즉, 이는 객체 포인터 배열이다. Point형의 자료의 주소를 담아둘수 있는 포인터배열을

    선언한것에 불과하다.

    그러므로, 각각 cell이 어떤 Point형의 객체를 가리키도록 연결해주어야 한다.

     

    class Point{
          int x;
          int y;
    public:
          Point(){
                cout<<"Point() called!" << endl;
                x=y=0;
          }
          Point(int _x, int _y){
                cout<<"Point(int, int) called!" << endl;
                x = _x;
                y = _y;
          }
    };

     

    int main(){
          Point* arr[3];
          cout<<"arr[3] created." << endl;

     

          arr[0] = new Point();
          arr[1] = new Point(1,2);                 
          arr[2] = new Point(3,4);
     

          delete arr[0];

          //delete arr[] 는 틀린표현. 
          return 0;
    }

    주소를 담을수 있는 공간을 만들었으니, 이제 그 공간에 주소를 넣어주자.

     

    각각의 cell에는 Point객체가 있으니  new연산자를 이용한다.

    (바로 이때, 그에 맞는 생성자가 호출된다.)

     

    new operator returns a suitably typed, nonzero pointer to the object.

    (new 연산자는 어떤 객체를 가리키는 0이 아닌 포인터를 리턴한다. )

     

    ===========================================================

     

    * 생각해보기1 *

     

    int* top[10];  과

    int* top = new int[10]; 는 다르다. -_-;

     

    int* top[10]는 top[0]~top[9]까지 각각의 cell들이 int를 가리키는 주소값을 넣을수 있게 선언됨.

    (즉, top은 포인터의 배열)

     

    int* top = new int[10]는 메모리 어딘가에 int형 공간을 10개 할당하고 그것의 최하위 공간의

    주소를 리턴해서 top에 저장.

     

     

    * 생각해보기2 *

     

    arr[0] = new Point();
    arr[1] = new Point(1,2);                 
    arr[2] = new Point(3,4); 

    delete arr[0];

     

    arr[0]에는 주소 0x001,

    arr[1]에는 주소 0x002,

    arr[2]에는 주소 0x003,

     

    이 저장되어 있다고 치고, delete arr[0] 이라고 해주면, arr[0]이 가리키고 있는 곳의 공간만

    해제가 되고, arr[0]에 들어있는 0x001이란 값은 그대로 유지 된다.

     

     

    ===========================================================

     

    this

     

    class Point{
           int x;
           int y;
    public:
           Point(){ 
                  x=y=0;
           }
           Point(int x, int y){ 
                  this->x = x;
                  this->y = y;
           }
           Point* getThis(){
                  return this;
           }
    };

     

    int main(){
           Point* a = new Point();

           cout << "a's address is " << a << endl;

           cout << "result of getThis() is " << a->getThis() << endl;

           return 0;
    }

     

    this는 객체 자신을 가리키는 포인터이다. 유식한 말로 "자기참조 포인터"라고 한다.

    main둘째줄과, 셋째줄의 결과는 똑같은 주소를 출력한다.

    셋째줄에서 pointer 변수 -> 함수 이렇게 쓰는걸 잊지 말자.

     

    그리고, Point클래스 내에서 자신의 멤버 변수는 this -> 변수이름 이렇게 가리킬수 있다.

    저런식으로 하면, 함수의 매개변수를 따로 생각할필요 없이 똑같이 써줘도 된다.

    (걍 머리좀 쓰는게 낫겠다 -_-;)

Designed by Tistory.