重发试试:A Polymorphic Product Creator.

本文介绍了多态产品创建器的实现,它允许使用名称作为模板参数创建对象,并返回基类类型的对象指针。给出了相关代码实现,还提到要防止调用特定函数,最后探讨了创建器的工作原理以及使用它的两个原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have create a article about this topic, but the all lettles pile together, so I rewrite it again. I create a polymorphic product creator which allows you to create objects using its name as a template argument, return a pointer to the object with its base class type. Here is the implementation: template struct AbstractCreator { virtual AbstractCreator* operator()(){throw();return 0;} }; Because we don't want to create a object which is base typed that unsually is a abstruct class, we have to prevent the creator from calling the function AbstractCreator::operator()().But we can not make it a pure function, the reason for which I will point out in the next article . template class CreatePolicy = Loki::CreateUsingNew > struct ProductCreator: public AbstructCreator { AbstructCreator* operator()() { return CreatePolicy::Create();} }; The code is so few but not so easy to understand. Let's have a view about how the creator works: We assum that class GTank and GBullet are derived from class GObject,then we can create codes like this: GObject* pO1 = ProductCreator()(); GObject* pO2 = ProductCreator()(); Why we need a ProductCreator? There are two reasons.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值