您的位置:首页 > 编程语言 > C语言/C++

C++动态创建对象

2017-05-24 14:22 351 查看
1.类似java中的反射

2.动态获取类型信息(方法与属性)

3.动态创建对象

  动态调用对象的方法

  动态操作对象的属性

  需要给每个类添加原数据

   对原有的类不做任何改,只需要增加一个宏就能够动态创建

4.动态创建对象

  1)避免if,else编写,增加扩展性

  2)将类名放到配置文件中,组件初步

  组件A,主程序组件A;

  后来出现组件B,主程序下载组件B,更新配置文件;主程序读取配置文件,获得方法

5.代码如下:添加REGISTER_CLASS(类名),即可动态创建

   sh make.sh生成可执行文件

   ./test

DynBase.h
#ifndef _DYNBASE_H_
#define _DYNBASE_H_

#include <map>
#include <string>
using namespace std;

typedef void* (*CREATE_FUNC)();

class DynObjectFactory {
public:
static void* CreateObject(const string& name) {
map<string, CREATE_FUNC>::const_iterator it;
it = mapCls_.find(name);
if(it == mapCls_.end()) {
return NULL;
} else {
//return it->second();
return (*it->second)();
}
}
static void  Register(const string& name, CREATE_FUNC func) {
mapCls_[name] = func;
}
private:
static map<string, CREATE_FUNC> mapCls_;
};

//__declspec(selectany) map<string, CREATE_FUNC> DynObjectFactory::mapCls_;
//map<string, CREATE_FUNC> DynObjectFactory::mapCls_;  // DynTest.cpp中使用DynBase.h;Shape.cpp中使用DynBase.h
__attribute((weak))map<string, CREATE_FUNC> DynObjectFactory::mapCls_;

class Register {
public:
Register(const string& name, CREATE_FUNC func) {
DynObjectFactory::Register(name, func);
}
};

#define REGISTER_CLASS(class_name)\
class class_name##Register {\
public:\
static void* NewInstance() {\
return new class_name;\
}\
private:\
static Register reg_;\
};\
Register class_name##Register::reg_(#class_name, class_name##Register::NewInstance);

#endif // _DYNBASE_H_


REGISTER_CLASS也可以使用模板

template<typename T>
class DelegatingClass {
public:
DelegatingClass(const string& name) {
DynObjectFactory::Register(name, &(DelegatingClass::NewInstance));
}

static void* NewInstance() {
return new T;
}

};

#define REGISTER_CLASS(class_name) DelegatingClass<class_name> class##class_name(#class_name)


Shape.h
#ifndef _SHAPE_H_
#define _SHAPE_H_
class Shape {
public:
virtual void Draw() = 0;
virtual ~Shape() {};
};

class Circle: public Shape {
public:
virtual void Draw() override;
virtual ~Circle() override;
};

class Square: public Shape {
public:
virtual void Draw() override;
virtual ~Square() override;
};

class Rectangle: public Shape {
public:
virtual void Draw() override;
virtual ~Rectangle() override;
};
#endif // _SHAPE_H_

Shape.cpp
#include "Shape.h"
#include "DynBase.h"
#include <iostream>
using namespace std;

void Circle::Draw() {
cout << "Circle::Draw()" << endl;
}

Circle::~Circle() {
cout << "~Circle ..." << endl;
}

void Square::Draw() {
cout << "Square::Draw()" << endl;
}

Square::~Square() {
cout << "~Square ..." << endl;
}

void Rectangle::Draw() {
cout << "Rectangle::Draw()" << endl;
}

Rectangle::~Rectangle() {
cout << "~Rectangle ..." << endl;
}

REGISTER_CLASS(Circle);
REGISTER_CLASS(Square);
REGISTER_CLASS(Rectangle);

/*
class CircleRegister {
public:
static void* NewInstance() {
return new Ciecle;
}
private:
static Register reg_;  //声明
};
Register CircleRegister::reg_("Circle", CircleRegister::NewInstance);
DynObjectFactory::Register("Circle", CircleRegister::NewInstance);
mapCls_["Circle"] = CircleRegister::NewInstance;
*/

DynTest.cpp
#include <iostream>
#include "Shape.h"
#include "DynBase.h"
#include <vector>
#include <string>
using namespace std;

void DrawAllShapes(const vector<Shape*>& v) {
vector<Shape*>::const_iterator it;
for(it = v.begin(); it != v.end(); ++it) {
(*it)->Draw();
}
}

void DeleteAllShapes(const vector<Shape*>& v) {
vector<Shape*>::const_iterator it;
for(it = v.begin(); it != v.end(); ++it) {
delete (*it);
}
}
int main() {
vector<Shape*> v;

Shape* ps;
ps = static_cast<Shape*>(DynObjectFactory::CreateObject("Circle"));
v.push_back(ps);
ps = static_cast<Shape*>(DynObjectFactory::CreateObject("Square"));
v.push_back(ps);
ps = static_cast<Shape*>(DynObjectFactory::CreateObject("Rectangle"));
v.push_back(ps);

DrawAllShapes(v);
DeleteAllShapes(v);

}

make.sh
#!/bin/bash
g++ Shape.cpp DynTest.cpp -o test -std=c++11
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: