C, C++2010. 9. 9. 22:28
반응형

''const pointer의 member function 호출은 const 형만 가능하다'

class  ConstTest
{
private: int a;
public: ConstTest() { a = 10; }
public:
    int get_const() const { return a; } /// 함수 선언과 정의 모두에 const가 필요함.
    int get() { return a; }
};

void test( const ConstTest* pConst, ConstTest* pNonConst )
{
    int ret1 = pConst->get_const(); /// OK
    int ret2 = pConst->get(); /// COMPILE ERROR

    int ret3 = pNonConst->get_const(); /// OK
    int ret4 = pNonConst->get(); /// OK
}

int main(int argc, _TCHAR* argv[])
{
    ConstTest ct1, ct2;
    test( &ct1, &ct2 );
    return 0;
}
반응형
Posted by Jay Two