有没有一种方法可以在不定义c++基类的情况下创建工厂
Is there a way to make a factory without defining a base class in c++?
我使用Qt,有一件事困扰着我,那就是我必须使用QObject才能使用qRegisterMetaType。我尝试自己实现工厂方法并取得了成功,但我仍然需要使用抽象类。
是否可以在不定义基类(在本例中为对象)的情况下做同样的事情?
工作代码:
#include <iostream>
#include <string>
#include <map>
#include <typeinfo>
#include <functional>
class object
{
public:
virtual ~object(){}
virtual std::string to_string() = 0;
};
class SomeObject : public object
{
public:
SomeObject(){}
virtual ~SomeObject(){}
virtual std::string to_string()
{
return "I am a type of SomeObject";
}
};
class SomeOtherObject : public object
{
public:
SomeOtherObject(){}
virtual ~SomeOtherObject(){}
virtual std::string to_string()
{
return "I am a type of SomeOtherObject";
}
};
std::map<std::string, std::function<object*()>> types;
template<typename O>
inline
static void register_type(const std::string & name)
{
types[name] = [](){ return new O; };
}
static object * get_object(const std::string & object_name )
{
return types[object_name]();
}
int main()
{
register_type<SomeObject>("SomeObject");
register_type<SomeOtherObject>("SomeOtherObject");
object * some = get_object("SomeObject");
object * some_other = get_object("SomeOtherObject");
std::cout << "::" << some->to_string() << std::endl;
std::cout << "::" << some_other->to_string() << std::endl;
delete some;
delete some_other;
std::cout << "exit" << std::endl;
return 0;
}
输出(需要-std=c++11):
::I am a type of SomeObject
::I am a type of SomeOtherObject
exit
您可以使用需要一个间接级别的类模板,但它不需要SomeObject
和SomeOtherObject
从object
派生。
#include <iostream>
#include <string>
#include <map>
#include <typeinfo>
#include <functional>
class object
{
public:
virtual ~object(){}
virtual std::string to_string() = 0;
};
template <typename RealObject>
class TemplateObject : public object
{
public:
TemplateObject() : ro_() {}
virtual std::string to_string()
{
return ro_.to_string();
}
private:
RealObject ro_;
};
std::map<std::string, std::function<object*()>> types;
template<typename O>
inline
static void register_type(const std::string & name)
{
types[name] = [](){ return new TemplateObject<O>(); };
}
static object * get_object(const std::string & object_name )
{
return types[object_name]();
}
class SomeObject
{
public:
SomeObject(){}
~SomeObject(){}
std::string to_string()
{
return "I am a type of SomeObject";
}
};
class SomeOtherObject
{
public:
SomeOtherObject(){}
~SomeOtherObject(){}
std::string to_string()
{
return "I am a type of SomeOtherObject";
}
};
int main()
{
register_type<SomeObject>("SomeObject");
register_type<SomeOtherObject>("SomeOtherObject");
object * some = get_object("SomeObject");
object * some_other = get_object("SomeOtherObject");
std::cout << "::" << some->to_string() << std::endl;
std::cout << "::" << some_other->to_string() << std::endl;
delete some;
delete some_other;
std::cout << "exit" << std::endl;
return 0;
}
输出:
::I am a type of SomeObject
::I am a type of SomeOtherObject
exit
在Amit的帮助下,我找到了这个解决方案:
#include <iostream>
#include <string>
#include <map>
#include <typeinfo>
#include <functional>
class object
{
public:
virtual ~object(){}
virtual std::string to_string() = 0;
};
class SomeObject : public object
{
public:
SomeObject(){}
virtual ~SomeObject(){}
virtual std::string to_string()
{
return "I am a type of SomeObject";
}
};
class SomeOtherObject : public object
{
public:
SomeOtherObject(){}
virtual ~SomeOtherObject(){}
virtual std::string to_string()
{
return "I am a type of SomeOtherObject";
}
};
std::map<std::string, std::function<void*()>> types;
template<typename O>
inline
static void register_type(const std::string & name)
{
types[name] = [](){ return new O; };
}
static void * get_object(const std::string & object_name )
{
return types[object_name]();
}
int main()
{
register_type<SomeObject>("SomeObject");
register_type<SomeOtherObject>("SomeOtherObject");
object * some = (object*)get_object("SomeObject");
object * some_other = (object*)get_object("SomeOtherObject");
std::cout << "::" << some->to_string() << std::endl;
std::cout << "::" << some_other->to_string() << std::endl;
delete some;
delete some_other;
std::cout << "exit" << std::endl;
return 0;
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何定义一个纯抽象基类