扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:李马编译 来源:VCKBASE 2007年10月19日
关键字:
#include <iostream> using namespace std; template <typename T> class Base { public: void fun() { cout << "Base::fun" << endl; } void doSomething() { T* pT = static_cast<T*>(this); pT->fun(); } }; class Drive : public Base<Drive> { public: void fun() { cout << "Drive::fun" << endl; } }; int main() { Drive obj; obj.doSomething(); return 0; } |
class Drive : public Base<Drive> { 这表明我们可以将Drive类作为一个模板参数来传递。程序中另外一个有趣的地方是基类中的doSomething函数。 T* pT = static_cast<T*>(this); pT->fun(); |
#include <iostream> using namespace std; template <typename T> class Base { public: void fun() { cout << "Base::fun" << endl; } void doSomething() { T* pT = static_cast<T*>(this); pT->fun(); } }; class Drive : public Base<Drive> { public: void fun() { cout << "Drive::fun" << endl; } }; class MostDrive : public Drive { public: void fun() { cout << "MostDrive::fun" << endl; } }; int main() { MostDrive obj; obj.doSomething(); return 0; } |
#include <iostream> using namespace std; template <typename T> class Base { public: void fun() { cout << "Base::fun" << endl; } void doSomething() { T* pT = static_cast<T*>(this); pT->fun(); } }; class Drive : public Base<Drive> { public: void fun() { cout << "Drive::fun" << endl; } }; int main() { Base* pBase = NULL; pBase = new Drive; return 0; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。