7.30(월) C++ - 함수객체, 주소연산자

from Study/C++ 2007/07/31 15:08 view 26544

#include <iostream>

using namespace std;

 

// 함수객체

// ()연산자를 재정의해서 함수처럼 동작하는 객체.

// 상태를 가지는 함수-> 함수 보다 훨씬 뛰어나다.

// 암시적인 inline을 갖는다.

// 함수보다 빠를 때가 있다. 일반 함수가 인자로 전달될 때 함수 포인터를 사용한다.
// (inline
을쓰지못한다.pointer이기 때문에.)

 

// 아래와 같은 경우는 지속적인 값을 가지지못한다. 매번값을넘겨줘야한다.

int plus( int a, int b, int ba )

{

        static int base = 0;

        base = ba;

        return a + b + base;

}

/////////////////////////////////////////////////////////////

 

class plus

{

        int base;

public:

        plus( int a = 0 ) : base(a) {}

 

        int operator()(int a, int b)

        {

               return a + b + base;

        }

};

 

void main()

{

        plus p(10);

 

        int s = p( 1, 2 );     // p.operator()(1,2)

 

        cout << s << endl;

}

 

// 컴파일러가만들어주는것들

 class Point

{

        int x, y;

 

public:

        void* operator&()

        {

               return this;

        }

 

        const void* operator&() const

        {

               return this;

        }

};

 

 

void main()

{

        const Point p;

 

        cout << &p << endl;;
        // p.operator&()
주소연산자도재정의가능하지만안하니못하다.!!

}

 

Tag |

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다