反射1--[c++ 宏定义 ## 的使用]

main.cpp

 1 #include <iostream>
 2 #include "register.h"
 3 #include "registerfun.h"
 4 
 5 using namespace std;
 6 
 7 
 8 int main()
 9 {
10     //1.使用##创建对象,创建同类,不同对象
11     cout << "1." <<endl;
12     Init();
13     Create("CObjectA");
14     Create("CObjectA");
15     Create("CObjectB");
16     
17     //2.使用##调用对象函数
18     cout << "2." <<endl;
19     InitFun();
20     CallFun("Welcome1");
21     CallFun("Welcome2");
22 
23     return 0;
24 }

register.h

 1 #ifndef _REGISTER_H_
 2 #define _REGISTER_H_
 3 
 4 #include <iostream>
 5 #include <string>
 6 
 7 #include "instance.h"
 8 
 9 extern"C" bool Init(void);
10 extern"C" bool Clear();
11 extern"C" IObj* Create(const string& name);
12 
13 #endif

register.cpp

#include "register.h"

#include <iostream>
#include <string>
#include <map>

using namespace std;

//宏定义的一种用法,可实现类似反射的 c++ 功能。宏定义的神奇,减少代码量,类似泛型的感觉
#define IMPLEMENT_CREATE_OBJ(classname)
    static IObj* _create_obj##classname()
    {
        return new classname();
    }
#define CREATE_OBJ_FUN(classname) _create_obj##classname

//函数指针
typedef IObj* (*FCreateObj)(void);
//函数指针的map类型
typedef map<string, FCreateObj> StrCreateFunMap;

//通过宏定义实现各种函数指针
IMPLEMENT_CREATE_OBJ(CObjectA)
IMPLEMENT_CREATE_OBJ(CObjectB)

//定义一个函数指针的map
StrCreateFunMap g_strCreateFunMap;

bool Init(void)
{
    g_strCreateFunMap["CObjectA"] = CREATE_OBJ_FUN(CObjectA);
    g_strCreateFunMap["CObjectB"] = CREATE_OBJ_FUN(CObjectB);
    return true;
}

bool Clear()
{
    return true;
}

IObj* Create(const string& name)
{
    return (*(g_strCreateFunMap[name]))();
}

registerfun.h

 1 #ifndef _REGISTER_FUN_H_
 2 #define _REGISTER_FUN_H_
 3 
 4 #include <iostream>
 5 #include <string>
 6 #include <map>
 7 
 8 #include "instance.h"
 9 
10 using namespace std;
11 
12 extern"C" bool InitFun(void);
13 extern"C" bool ClearFun(void);
14 extern"C" bool CallFun(const string& name);
15 
16 #endif

registerfun.cpp

#include "registerfun.h"

#include <iostream>
#include <string>
#include <map>

using namespace std;

#define IMPLEMENT_CALL_FUN(funname)
    static void _call_instance##funname()
    {
        CInstance* p = CInstance::GetInstance();
        p->funname();
    }
#define CALL_FUN(funname) _call_instance##funname

typedef void(*FCallFun)(void);
typedef map<string, FCallFun> StrCallFunMap;

IMPLEMENT_CALL_FUN(Welcome1)
IMPLEMENT_CALL_FUN(Welcome2)

StrCallFunMap g_strCallFunMap;

bool InitFun()
{
    g_strCallFunMap["Welcome1"] = CALL_FUN(Welcome1);
    g_strCallFunMap["Welcome2"] = CALL_FUN(Welcome2);
    return true;
}

bool ClearFun()
{
    return true;
}

bool CallFun(const string& name)
{
    (*(g_strCallFunMap[name]))();
    return true;
}

instance.h

 1 #ifndef _INSTANCE_H_ 
 2 #define _INSTANCE_H_
 3 
 4 #include <iostream>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 class IObj
10 {
11 public:
12     virtual void Welcome()=0;
13 };
14 class CObjectA : public IObj
15 {
16 public:
17     CObjectA();
18     virtual ~CObjectA();
19     void Welcome();
20 };
21 class CObjectB : public IObj
22 {
23 public:
24     CObjectB();
25     virtual ~CObjectB();
26     void Welcome();
27 };
28 class CInstance
29 {
30 private:
31     CInstance();
32     virtual ~CInstance();
33 public:
34     static CInstance* GetInstance();
35     void Welcome1();
36     void Welcome2();
37 private:
38     static CInstance* m_pInstance;
39 };
40 
41 #endif

instance.cpp

#include "instance.h"

//
CObjectA::CObjectA()
{
    Welcome();
}
CObjectA::~CObjectA()
{
}
void CObjectA::Welcome()
{
    cout << "CObjectA welcome " << this <<endl;
}

//
CObjectB::CObjectB()
{
    Welcome();
}
CObjectB::~CObjectB()
{
}
void CObjectB::Welcome()
{
    cout << "CObjetcB Welcome " << this <<endl;
}

//静态私有类成员变量赋值
CInstance* CInstance::m_pInstance = NULL;
CInstance::CInstance() 
{
}
CInstance::~CInstance()
{
}
CInstance* CInstance::GetInstance()
{
    if (m_pInstance == NULL) {
        m_pInstance = new CInstance();
    }
    return m_pInstance;
}
void CInstance::Welcome1()
{
    cout << "CInstance Welcome1"<<endl;
}
void CInstance::Welcome2()
{
    cout << "CInstance Welcome2"<<endl;
}
原文地址:https://www.cnblogs.com/kobe-vanessa/p/5453657.html