飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 3217|回复: 1

[C/C++] 模拟虚函数机制

[复制链接]
  • TA的每日心情
    郁闷
    2017-1-19 11:18
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2012-2-22 00:15:39 | 显示全部楼层 |阅读模式
    1. // Base.h: interface for the Base class.
    2. //
    3. //////////////////////////////////////////////////////////////////////
    4. #if !defined(AFX_BASE_H__BC0F931E_FB88_46BB_AACF_81A971F3B966__INCLUDED_)
    5. #define AFX_BASE_H__BC0F931E_FB88_46BB_AACF_81A971F3B966__INCLUDED_
    6. #if _MSC_VER > 1000
    7. #pragma once
    8. #endif // _MSC_VER > 1000
    9. #include <iOStream.h>
    10. #include <memory.h>
    11. class Base;
    12. typedef void (Base::*FUNC_TYPE1)();
    13. class Base  
    14. {
    15. public:
    16.     static FUNC_TYPE1 m_VTable[];
    17.     FUNC_TYPE1       *m_lpVTable;
    18. public:
    19.     Base();
    20.     ~Base();
    21. public:
    22.     void vsSayHello()
    23.     {
    24.         cout << "BASE::SAYHELLO" << endl;
    25.     }
    26.     void vsSayGoodBye()
    27.     {
    28.          cout << "BASE::SAYGOODBYE" << endl;
    29.     }
    30.    
    31. };

    32. #endif // !defined(AFX_BASE_H__BC0F931E_FB88_46BB_AACF_81A971F3B966__INCLUDED_)
    复制代码
    1. // Base.cpp: implementation of the Base class.
    2. //
    3. //////////////////////////////////////////////////////////////////////
    4. #include "stdafx.h"
    5. #include "Base.h"
    6. //////////////////////////////////////////////////////////////////////
    7. // Construction/Destruction
    8. //////////////////////////////////////////////////////////////////////
    9. FUNC_TYPE1 Base::m_VTable[] = {
    10.     Base::vsSayHello,
    11.     Base::vsSayGoodBye
    12. };
    13. Base::Base()
    14. {
    15.     m_lpVTable = Base::m_VTable;
    16. }
    17. Base:: ~Base()
    18. {
    19.    
    20. }
    复制代码
    1. // Derive.h: interface for the Derive class.
    2. //
    3. //////////////////////////////////////////////////////////////////////
    4. #if !defined(AFX_DERIVE_H__FB413D6B_2A11_40FB_9B58_E32FEF4FA050__INCLUDED_)
    5. #define AFX_DERIVE_H__FB413D6B_2A11_40FB_9B58_E32FEF4FA050__INCLUDED_
    6. #if _MSC_VER > 1000
    7. #pragma once
    8. #endif // _MSC_VER > 1000
    9. #include "Base.h"
    10. class Derive;
    11. typedef void (Derive::*FUNC_TYPE2)();
    12. class Derive : public Base  
    13. {
    14. public:
    15.     static FUNC_TYPE2 m_VTable[];
    16. public:
    17.     Derive();
    18.     ~Derive();
    19. public:
    20.     void vsSayHello()
    21.     {
    22.         cout << "Derive::SAYHELLO" << endl;
    23.     }
    24.     void vsSayGoodBye()
    25.     {
    26.         cout << "Derive::SAYGOODBYE" << endl;
    27.     }
    28.    
    29. };

    30. #endif // !defined(AFX_DERIVE_H__FB413D6B_2A11_40FB_9B58_E32FEF4FA050__INCLUDED_)
    复制代码
    1. // Derive.cpp: implementation of the Derive class.
    2. //
    3. //////////////////////////////////////////////////////////////////////
    4. #include "stdafx.h"
    5. #include "Derive.h"
    6. //////////////////////////////////////////////////////////////////////
    7. // Construction/Destruction
    8. //////////////////////////////////////////////////////////////////////
    9. FUNC_TYPE2 Derive::m_VTable[] = {
    10.     Derive::vsSayHello,
    11.     Derive::vsSayGoodBye
    12. };
    13. Derive::Derive()
    14. {
    15.     memcpy(Derive::m_VTable, m_lpVTable, 4);
    16.     m_VTable[0] = Derive::vsSayHello;
    17.     m_VTable[1] = Derive::vsSayGoodBye;
    18.     m_lpVTable = (FUNC_TYPE1 *)Derive::m_VTable;   
    19. }
    20. Derive:: ~Derive()
    21. {
    22.    
    23. }
    复制代码
    1. // 模拟虚函数机制.cpp : Defines the entry point for the console application.
    2. //
    3. #include "stdafx.h"
    4. #include "Derive.h"
    5. void CallFunction(Base* lpBase)
    6. {
    7.     (lpBase->*(lpBase->m_lpVTable[0]))();
    8.     (lpBase->*(lpBase->m_lpVTable[1]))();
    9. }
    10. int main(int argc, char* argv[])
    11. {
    12.     Base theBase;
    13.     Derive theDerive;
    14.     CallFunction(&theBase);
    15.     CallFunction(&theDerive);
    16.     return 0;
    17. }
    复制代码
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2023-9-22 17:19
  • 签到天数: 775 天

    [LV.10]以坛为家III

    发表于 2012-2-22 00:48:33 | 显示全部楼层
    /:002 不会c,看不懂……
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表