二重クラスの定義
class MainClass{
public:
MainClass();
~MainClass();
class SubClass {
public:
SubClass();
~SubClass(){}
};
};
MainClass::MainClass(){
SubClass subclass;
}
MainClass::~MainClass(){
}
MainClass::SubClass::SubClass(){
}
int main(){
MainClass mainclass;
MainClass::SubClass subclass;
return 0;}
なるほど大規模なソフトでよく見る奴はこうしてたのか. 静的多様化をつかってメタ化できるかな ? ちょっとやってみる.
追記
やってみたらあっさり出来た. これはとっても便利だ.
#include<iostream>
template< typename DerivedMainClass >
class MainClass{
public:
MainClass(){};
~MainClass(){};
bool CallFunc(){
return static_cast<DerivedMainClass*>(this)->CallMainFuncImple();
}
template< typename DerivedSubClass >
class SubClass {
public:
bool CallFunc (){
return static_cast<DerivedSubClass*>(this)->CallSubFuncImple();
}
SubClass(){};
~SubClass(){}
};
};
class MainClassDerived : public MainClass <MainClassDerived>{
friend class MainClass < MainClassDerived >;
bool CallMainFuncImple(){
std::cout << "Derived main Class" << std::endl;
}
};
class SubClassDerived : public MainClass<MainClassDerived>::SubClass <SubClassDerived> {
friend class SubClass < SubClassDerived >;
bool CallSubFuncImple(){
std::cout << "Derived sub Class" << std::endl;
}
};
int main(){
MainClass<MainClassDerived> *mainclass = new MainClassDerived();
MainClass<MainClassDerived>::SubClass<SubClassDerived> *subclass = new SubClassDerived();
mainclass->CallFunc();
subclass->CallFunc();
return 0;}
0 件のコメント:
コメントを投稿