Game !

 

함수 객체 : ( ) 연산자 오버로딩을 사용해서 객체를 함수처럼 쓰는 문법

보통 operator( ) 오버로딩 함수 내부에 인라인화를 시켜서 호출 시에 임시 메모리에 들어가도록 의도한다.

 

하지만 미리 객체를 생성해놓고 필요할 때 사용하는 것이므로

Stack 영역이든 어디든간에 결국 메모리를 사용하는 구조이다.

 

▼ 함수 객체 예시

더보기
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class CSortRule
{
public:
    virtual bool operator()(int a, int b) = 0;
};
 
class CAsendingSort : public CSortRule
{
public:
    bool operator()(int a, int b)
    {
        return a > b;
    }
};
 
class CDesendingSort : public CSortRule
{
public:
    bool operator()(int a, int b)
    {
        return a < b;
    }
 
};
 
void    Bubble_Sort(int iArray[], int _iSize, CSortRule& Functor)
{
    for (int i = 0; i < _iSize; ++i)
    {
        for (int j = 0; j < _iSize - 1++j)
        {
            if (Functor(iArray[j], iArray[j + 1]))    // 여기서 함수 객체가 사용된다.
            {
                //...
            }
        }
    }    
}
 
void main()
{    
    int iArray[5= { 514 ,32 };
 
    CAsendingSort        Asending;
    CDesendingSort        Desending;
 
    Bubble_Sort(iArray, 5, Desending);
 
}
cs

 

 

임시 객체 : 이름 없이 임시 메모리에 등록되는 객체. 그 라인이 끝나면 객체는 즉시 소멸된다.

일반 객체와 달리 메모리에 상주해있지 않는다. 

 

▼ 임시 객체 예시

더보기
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
class CTest
{
public:
    CTest(const char* pStr)
    {
        strcpy_s(m_szStr, sizeof(m_szStr), pStr);
        cout << m_szStr << "_생성자 호출" << endl;
    }
    ~CTest()
    {
        cout << m_szStr << "_소멸자 호출" << endl;
    }
 
private:
    char        m_szStr[64];
};
 
 
void main(void)
{
    CTest        Temp("일반 객체");
 
    cout << "==============임시 객체 생성==============" << endl;
    CTest("임시 객체");
    cout << "==============임시 객체 소멸==============" << endl;
 
    cout << "_SUCY_" << endl;
 
 
}
cs