【c++】用工厂模式实现计算器功能(附工厂模式代码)

本文通过C++编程,详细讲解如何利用工厂模式设计并实现一个计算器的功能。在Linux环境下,使用vim编辑器进行代码编写,重点介绍了工厂模式的应用。
摘要由CSDN通过智能技术生成

工厂模式

编译环境:Linux vim

DynBase.h

#ifndef _DYN_BASE_H_
#define _DYN_BASE_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 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)) 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() \
	{ \
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值