首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++不要虚函数实现多态

2013-08-01 
C++不用虚函数实现多态原文:http://emowuyi.iteye.com/blog/1324159?#include stdafx.htypedef void (*f

C++不用虚函数实现多态

原文:http://emowuyi.iteye.com/blog/1324159

?

#include "stdafx.h"typedef void (*fVoid)();class A{public:        static void test()        {                printf("hello A\n");        }        fVoid print;        A()        {                print = A::test;        }};class B : public A{public:        static void test()        {                printf("hello B\n");        }        B()        {                print = B::test;        }};int main(){        A aa;        aa.print();        B b;        A* a = &b;        a->print();        getchar();        return 0;}

?

这样做的好处主要是绕过了vtable。我们都知道虚函数表有时候会带来一些性能损失。

热点排行