클래스 템플릿을 기반클래스로 상속받는 경우에 기반클래스의 타입인자에 해당하는 멤버를 호출하려면
글쓴이: dltkddyd / 작성시간: 화, 2014/06/03 - 4:48오후
어떻게 해야 하나요.
using이나 this를 사용하라고 하던데요. 그렇게 하지 않으면 컴파일 오류가 난다고 하더군요. 그런데 그렇게 하지 않아도 아래의 경우는 컴파일 오류가 발생하지 않는데요.
class A {
//정의
};
class B {
//정의
};
template<class T>
class Base {
//정의
};
//저 T에넌 A나 B 클래스를 전달할 것으로 예상합니다.
template<class T>
class Derived:public Base<T> {
//정의
};이거 컴파일이 제대로 되지 않는다고 합니다. 저 Base 클래스 템플릿이 특수화되면 멤버가 없을 수도 있기 때문에 컴파일이 되지 않도록 한다라고 하는데요. 그 해결방법으로 Base 클래스 안에 using을 다음과 같이 안에서 사용하라고 하더군요.
public: using Base<T>::특수화에서 선언과 정의하지 않은 함수
Base의 일반화된 버전에서는 dFunction이 있으나 특수화된 Base에서는 dFunction이 없다고 할 때, 컴파일이 안되니 위와 같이 파생클래스에서 언급하라고 합니다. 그런데 using Base의 T도 Zclass가 되면 거기에도 역시 dFunction이 없는데, 저 using를 언급한다고 컴파일 오류가 안 나는 이유는 뭔가요? 저 using의 T라는 것이 컴파일시에 박히는 것이라서 그런건가요?
Forums:


구체적으로 이런
구체적으로 이런 경우인데요.
-binherit.h-
class SeoulBranch { public: int revenue; int cost; SeoulBranch(); SeoulBranch(int _revenue, int _cost); SeoulBranch(const SeoulBranch& right); int setRevenue(int _revenue); int setCost(int _cost); int getRevenue(); int getCost(); }; SeoulBranch::SeoulBranch():revenue(0),cost(0) {} SeoulBranch::SeoulBranch(int _revenue, int _cost):revenue(_revenue), cost(_cost) {} SeoulBranch::SeoulBranch(const SeoulBranch& right):revenue(right.revenue), cost(right.cost) {} int SeoulBranch::setRevenue(int _revenue) { revenue=_revenue; }; int SeoulBranch::setCost(int _cost) { cost=_cost; }; int SeoulBranch::getRevenue() {return revenue;} int SeoulBranch::getCost() {return cost;} class ChungChong { public: int revenue; int cost; ChungChong(); ChungChong(int revenue, int cost); ChungChong(const ChungChong& right); int setRevenue(int _revenue); int setCost(int _cost); //int getRevenue(); int getCost(); }; ChungChong::ChungChong():revenue(0), cost(0){}; ChungChong::ChungChong(int _revenue, int _cost):revenue(_revenue), cost(_cost){}; ChungChong::ChungChong(const ChungChong& right):revenue(right.revenue), cost(right.cost) {} int ChungChong::setRevenue(int _revenue) { revenue=_revenue; }; int ChungChong::setCost(int _cost) { cost=_cost; }; //int ChungChong::getRevenue() {return revenue;} int ChungChong::getCost() {return cost;} template <class BranchType> class Support { public: int bonus; int goal; BranchType a; Support(); Support(int _goal, int _bonus); Support(const Support& right); }; template<class BranchType> Support<BranchType>::Support():bonus(0), goal(0) {} template<class BranchType> Support<BranchType>::Support(int _goal, int _bonus=0):goal(_goal),bonus(_bonus) {}; template<class BranchType> Support<BranchType>::Support(const Support& right):bonus(right.bonus), goal(right.goal) {} template<class BranchType> class Msg:public Support<BranchType> { public: Msg(); Msg(int goal); Msg(const Msg& right); }; template<class BranchType> Msg<BranchType>::Msg():Support<BranchType>() {}; template<class BranchType> Msg<BranchType>::Msg(int _goal):Support<BranchType>(_goal) {}; template<class BranchType> Msg<BranchType>::Msg(const Msg& right):Support<BranchType>(right) {};-test5.cc-
#include "binherit.h" int main() { Msg<SeoulBranch> seoul1(6000000); return 0; }ChungChong에 getRevenue가 없어도 책 내용(Effective C++ 311쪽)과는 달리 컴파일이 되는데요.
본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.
댓글 달기