【C++】动态创建对象

动态创建对象

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

#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)

宏展开

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

Shape.h

#pragma once
#include <iostream>
#include <vector>
using namespace std;
class Shape
{
public:
    virtual void Draw() = 0;
    virtual ~Shape() {};
};

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


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

class Rectangle :public Shape
{
public:
    void Draw();
    ~Rectangle();
};

Shape.cpp

#include "Shape.h"
#include <iostream>
#include <vector>
#include "DynObjectFactory.h"
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 Circle;
    }
private:
    static Register reg_;//声明
}
Register CircleRegister::reg_("Circle",CircleRegister::NewInstance)
*/


/*宏

#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)

*/

DynObjectFactory.h

#pragma once
#include <iostream>
#include <string>
#include <map>
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 0;
		else
			return it->second();
	}

	static void  Register(const string& name, CREATE_FUNC func)
	{
		mapCls_[name] = func;
	}
private:
	static map<string, CREATE_FUNC> mapCls_;
};

//g++
//__attribute((weak))
_declspec(selectany) 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)

DynObjectFactory.cpp

#include "DynObjectFactory.h"
#include <iostream>
#include <vector>
#include "Shape.h"
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);
    return 0;
}

你可能感兴趣的:(C++,c++,开发语言)