gpt4 book ai didi

c++ - 有没有办法在不在 C++ 中定义基类的情况下创建工厂?

转载 作者:行者123 更新时间:2023-11-28 05:54:55 24 4
gpt4 key购买 nike

我使用 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

最佳答案

您可以使用需要一级间接的类模板,但它避免了 SomeObjectSomeOtherObjectobject 派生的需要。

#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

关于c++ - 有没有办法在不在 C++ 中定义基类的情况下创建工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34386672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com