求大神帮我看下,这个问题我困扰了半个月

头文件1 dynbase.h

#ifndef   _DYNBASE_H_
#define   _DYNBASE_H_

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

typedef void* (*CREATE_FUNC)();


class DynCreateFactory {
public:
	/* ----- 从map容器中得到响应的形状 ----- */
	static void* CreateShape(const string& name);
	/* ----- 运行时动态创建各类形放入map容器 ----- */
	static void registes(const string& name, CREATE_FUNC func);
private:
	static map<string, CREATE_FUNC> cls_;
};

/* ----- 利用构造函数自动调用DynCreateFactory中的registes函数 ----- */
class RegistObj {
public:
	RegistObj(const string& name, CREATE_FUNC func);
};

/* ----- 利用宏定义在main前创建工厂 ----- */
#define CREATE_OBJ(class_name) \
class class_name##Registe { \
public: \
	static void* Instance() { \
		return new class_name; \
    } \
private: \
	static RegistObj reg_; \
}; \
RegistObj  class_name##Registe::reg_(#class_name, class_name##Registe::Instance)

#endif // _DYNBASE_H_




头文件2 shape.h

#ifndef   _SHAPE_H_
#define   _SHAPE_H_

#include "dynbase.h"
#include <iostream>
using namespace std;

class Shape {
public:
	virtual void Draw() = 0;
	virtual ~Shape() {}
};

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

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

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

#endif // _SHAPE_H_ 






源文件1 dynbase.cpp

#include "dynbase.h"

map<string, CREATE_FUNC> DynCreateFactory::cls_;

void* DynCreateFactory::CreateShape(const string& name) {
	map<string, CREATE_FUNC>::iterator it;

	it = cls_.find(name);
	if (it == cls_.end())
		return 0;
	else
		return it->second();
}

void DynCreateFactory::registes(const string& name, CREATE_FUNC func) {
	cls_[name] = func;
}

RegistObj::RegistObj(const string& name, CREATE_FUNC func) {
	DynCreateFactory::registes(name, func);
}





源文件2 shape.cpp

#include "shape.h"

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

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

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

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

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

CREATE_OBJ(Circle);
//CREATE_OBJ(Rectangle);
//CREATE_OBJ(Square);





源文件3 main.cpp

#include "shape.h"

int main(void) {
	Shape* sp = (Shape*)DynCreateFactory::CreateShape("Circle");
	sp->Draw();
	delete sp;

	return 0;
}

#C++工程师#
全部评论

相关推荐

现在是2026.2.27,距离我2025.8.16在boss上投出第一份简历以来已经过去了半年多时间了。可能许多牛友对我并不陌生,在去年的89月份,深陷实习焦虑的我不停的在牛客上发帖求助,改过的简历不知道发了多少次。因为双非本的缘故,在实习这条路上可谓是处处碰壁。boss上四位数的沟通只换来两位数的回复,好不容易约到的面试很多还因为各种原因被挂。最终在9月底遇到了我实习过程中的第一个贵人:美团实习的ld。尽管那是个测开岗,但是没有关注我实际的技术栈,而是用专业的提问让我感受到了前所未有的面试体验,发掘了自己的技术闪光点。最终让我决定放弃了另一家中小厂的后端。他们非常尊重我对开发学习的热情,也给足了我自由发挥的空间,如果不是他们让我深度参与的用例生成项目,我或许连接到后面面试的机会都没有。尽管岗位不是开发,但这个过程中对大厂工作流程的深度参与以及对业务,项目,和技术的思维提升对我后续的开发面试一样提供了巨大的帮助。时代的洪流让我们每个人都被迫卷入其中,错过了互联网的红利时期,无论实习还是秋招都令不同背景的同学倍感压力,尽管如此我们依旧要相信:努力定有回报最后祝各位27的兄弟姐妹们,在暑期实习的面试路上一路披荆斩棘,策马扬鞭,用梦中情司的offer回应自己一直以来不愿放弃的拼搏timeline:2.6一面2.11&nbsp;二面2.12&nbsp;三面&nbsp;当天转hr面2.26&nbsp;hr面,面完云证+录用评估2.27&nbsp;offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务