C++惯用法:奇特的递归模板模式
2017-11-07 15:42
387 查看
using namespace std; template<class Derived> struct Base { void Interface() { cout <<"come from Interface"<<endl; // 转换为子类指针,编译期将绑定至子类方法 static_cast<Derived*>(this)->Implementation(); } static void StaticInterface() { // 编译期将绑定至子类方法 cout <<"come from StaticInterface"<<endl; Derived::StaticImplementation(); } void Implementation() { cout <<"Base Implementation"<<endl; return; } static void StaticImplementation() { cout << "Base StaticImplementation"<<endl; return; } }; // The Curiously Recurring Template Pattern (CRTP) struct Derived1 : Base<Derived1> { static void StaticImplementation(); }; struct Derived2 : Base<Derived2> { void Implementation(); }; void Derived1::StaticImplementation() { cout << "StaticImplementation from Derived1"<<endl; return; } void Derived2::Implementation() { cout <<"Implementation from Derived2"<<endl; return; } int main(){ cout << "***********************************" << endl; Derived1 derive1; Derived2 derive2; derive1.Implementation(); derive1.StaticImplementation(); derive2.Implementation(); derive2.StaticImplementation(); cout << "***********************************" << endl << endl; Base<Derived1> base_derive1; Base<Derived2> base_derive2; base_derive1.Implementation(); base_derive1.StaticImplementation(); base_derive2.Implementation(); base_derive2.StaticImplementation(); cout << "***********************************" << endl << endl; base_derive1.StaticInterface(); base_derive1.Interface(); base_derive2.StaticInterface(); base_derive2.Interface(); cout << "***********************************" << endl << endl; return 0; }
相关文章推荐
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++ 模板:奇特递归模板模式(Curiously Recurring Template Pattern -CRTP)和 静多态(Static polymorphism)
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- templates — C++模板的应用<奇特的递归模板模式>
- templates — C++模板的应用<奇特的递归模板模式>
- 模板与继承之艺术——奇特的递归模板模式(CRTP)
- 奇特递归模板模式
- 奇特递归模板模式
- STL源码剖析 05 基础-CRTP奇特的递归模式模板 子类作为父类的模板参数
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- 类对象引用计数使用模板实现(奇特的递归模板模式)
- c++模板递归单例模式进阶版本
- C++ 模板的用法
- 设计模式_模板方法模式(C++)
- 设计模式---模板模式(C++实现)
- 从零开始学C++之对象的使用(三):static 与单例模式、auto_ptr与单例模式、const 用法小结、mutable修饰符
- 设计模式C++实现(5)——原型模式、模板方法模式
- 07 C++模板特化、模板偏特化和模板模板参数基本用法
- c++ 实现模板方法模式