atl/wtl中运用的非虚函数多态
template <typename T> class B1 { public: void SayHi() { T *pT = static_cast<T*>(this); pT->PrintClassName(); } protected: void PrintClassName() { cout << "B1" << endl; } }; class D1 : public B1<D1> { // no overridden function at all }; class D2 : public B1<D2> { public: void PrintClassName() { cout << "D2" << endl; } }; void testAtlWtl() { D1 d1; D2 d2; d1.SayHi(); d2.SayHi(); }
这是一种运用模板机制实现c++中多态的一种方法,避免了虚函数的vtable的开销,是理解atl/wtl的最基本的原理。
原文链接: https://www.cnblogs.com/SunWentao/archive/2011/10/03/2198515.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/33491
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!