// dynCreateBase.h
#pragma once
#include <map>
#include <string>
using namespace std;
typedef void* (*CREATE_FUNC)();
class Register;
class DynObjectFactory {
public:
static void* CreateObject(const string& name);
static void Register(const string& name, CREATE_FUNC func);
private:
static map<string, CREATE_FUNC> mapCls;
};
class Register {
public:
Register(const string& name, CREATE_FUNC func);
};
// 用謷牙诘屈的宏,定义类。谁能告诉我,这样的宏怎么注释
// #:它是一个指示符,指示其及后面的变量名替代一个字符串;
// ##:它是一个连接符,表示它和他后面的变量名合并在一起。
// class_name##Register 表示类名 ##是class_name与Regigister类名的连接符
// Register class_name##Register::reg(#class_name, class_name##Register::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)
// 用类名Circle来做例子
// Register CircleRegister::reg(Circle, CircleRegister::NewInstance)
#include "dynCreateBase.h"
// g++
// __attribute ((weak))
__declspec(selectany) map<string, CREATE_FUNC> DynObjectFactory::mapCls;
//头文件被包含多次,也只定义一次mapCls_;
void* DynObjectFactory::CreateObject(const string& name) {
map<string, CREATE_FUNC>::const_iterator it = mapCls.find(name);
if (it == mapCls.end())
return 0;
else
return it->second(); //func();
}
void DynObjectFactory::Register(const string& name, CREATE_FUNC func) {
DynObjectFactory::mapCls[name] = func;
}
Register::Register(const string& name, CREATE_FUNC func) {
DynObjectFactory::Register(name, func);
}
// shape.h
#pragma once
class Shape { // 虚基类
public:
// 对于形状这种类,谁也画不出来。so...就让绘图接口成为纯虚函数
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();
};
#include "shape.h"
#include "dynCreateBase.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);
// main.cpp
#include "shape.h"
#include "dynCreateBase.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void DrawAllShapes(const vector<Shape*>& v) {
for (vector<Shape*>::const_iterator 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);
}
}
Shape* CreateClass(const string& strName) {
return static_cast<Shape*>(DynObjectFactory::CreateObject(strName));
}
int main(void) {
vector<Shape*> v;
Shape* ps;
ps = CreateClass("Circle");
v.push_back(ps);
ps = CreateClass("Square");
v.push_back(ps);
ps = CreateClass("Rectangle");
v.push_back(ps);
DrawAllShapes(v);
DeleteAllShapes(v);
return 0;
}
%%%% 一路追随大佬到这里 能不能推荐几本好的技术书qaq
简单类工厂。java有映射机制,但是C++没有。只有这么弄。MFC早年的技术。
弱弱的问一句:为啥要这么写宏呢