template base class 호출하기

Tags:

EC++ 3rd ed. 에 있는 내용중 item 43의 요약입니다. 소스는 제가 간단한걸로 새로 짜봄.

#include <iostream>

using namespace std;

template<typename T>
class Parent
{
public:
    void method()
    {
        cout <"Parent::method" << endl;
    }
};

template<typename T>
class Childpublic Parent<T>
{
public:
    void method2()
    {
        cout <"Child::method2" << endl;
        this->method();
    }
};

int main(int argc, string argv)
{
    Child<int> c;
    c.method2();
    return EXIT_SUCCESS;
}

Child의 method2에서 this-> 이 없는 경우 다음과 같은 에러가 납니다.

[root@protos tmp]# !g
g++ test.cpp
test.cpp: In member function ‘void Child::method2()’:
test.cpp:22: error: there are no arguments to ‘method’ that depend on a template parameter, so a declaration of ‘method’ must be available
test.cpp:22: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

이는 child에서 template인 base를 상속받을때 비록 타입 T를 넘겨주긴 하지만, 이 넘겨받은 타입으로 base가 specialized 될 경우 베이스에는 method() 라는 메소드가 없을 수 있기 때문입니다. 그래서 generalized template base class의 내부는 보지 않습니다. 해결책은 여기에 적은 듯이 this-> 를 사용해서 method를 호출하면 됩니다.

p.s.
내용은 이렇고.. 하루하루 item 한두개씩 읽곤 하고 있는데 정말 이런 이상한 변칙 룰들 보면 뭐가 이렇게 어처구니 없는 언어가 다있나 싶습니다;; effectiveness를 위해 this-> 를 붙일 경우에만 컴파일할때 시간을 조금 더 들여보겠다 이런거 같은데, 진짜 C++ 의 몇몇 특성들은 ‘즐’ 입니다;;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *