gpt4 book ai didi

C++ 对象构造函数数组

转载 作者:太空狗 更新时间:2023-10-29 23:49:47 25 4
gpt4 key购买 nike

我有几个名为 Child1、Child2 等的类,继承自对象 Parent。我需要根据名称创建一个对象,例如,如果我有字符串“Child1”,我需要创建对象 Child1 等等。

我想过类似的事情:

struct registry_entry {
const char* name;
IREGISTRY* (*initializer)();
};

struct registry_entry registry_list[] =
{
{"xml", &REGISTRY_XML::REGISTRY_XML},
}

但是我找不到对象构造函数的地址。我相信这个问题一定已经解决了,解决方案也很简单,但我找不到。

最佳答案

构造函数和析构函数是特殊函数,不能通过函数指针访问。

你需要像这样创建一个静态成员函数

struct REGISTRY_XML {
static IREGISTRY* create();
};

因此您可以将其称为

struct registry_entry registry_list[] =
{
{"xml", &REGISTRY_XML::create},
}

关于C++ 对象构造函数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963992/

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