C++反射机制_测试类

 

 

 

C++反射机制_#include_02

 

 

 

C++反射机制_C_03

 

 

 

C++反射机制_#include_04

 

 

 

测试如下:

 

 

#include "ObjectReflect.h"

//测试类1
class MyObject1
{
public:
MyObject1();
~MyObject1();

void TestFunc();
private:
static ObjectReflect<MyObject1> m_objectReflect;
};

 

#include "MyObject1.h"

ObjectReflect<MyObject1> MyObject1::m_objectReflect("MyObject1");

MyObject1::MyObject1()
{
printf("MyObject1::MyObject1()\n");
}


MyObject1::~MyObject1()
{
}

void MyObject1::TestFunc()
{
printf("MyObject1::TestFunc()\n");
}

//----------------------------------------------------------------------------------------------------------------------

//测试类2
class MyObject2
{
public:
MyObject2();
~MyObject2();

int Add(int a, int b);
private:
static ObjectReflect<MyObject2> m_objectReflect;
};

#include "MyObject2.h"


ObjectReflect<MyObject2> MyObject2::m_objectReflect("MyObject2");

MyObject2::MyObject2()
{
printf("MyObject2::MyObject2()\n");
}


MyObject2::~MyObject2()
{
}

int MyObject2::Add(int a, int b)
{
return a + b;
}

//----------------------------------------------------------------------------------------------------------------------

#include "ObjectFactory.h"
#include "MyObject1.h"
#include "MyObject2.h"
#include <assert.h>

void main()
{
auto t1 = ObjectFactory::Instance()->CreateObject("MyObject1");
auto t11 = static_cast<MyObject1*>(t1);
t11->TestFunc();
printf("-------------------------------\n");
auto t2 = ObjectFactory::Instance()->CreateObject("MyObject2");
printf("-------------------------------\n");
auto t3 = ObjectFactory::Instance()->CreateObject("MyObject1");
assert(ObjectFactory::Instance()->CreateObject("MyObject3") == nullptr);

getchar();
}

//完整代码引用:https://github.com/6VV/CppReflect