创建指向成员函数的指针映射
Creation of a map of pointer to member function
我正在尝试创建一个由int
和指向成员函数的指针组成的映射。
class Factory
{
public:
typedef nts::IComponent *(*createFunction)(const std::string &value);
Factory();
~Factory();
nts::IComponent *createComponent(const std::string &type, const std::string &value);
private:
nts::IComponent *create4001(const std::string &value) const;
nts::IComponent *create4013(const std::string &value) const;
nts::IComponent *create4040(const std::string &value) const;
nts::IComponent *create4081(const std::string &value) const;
std::map<int, createFunction> map = {{4001, Factory::create4001},
{4013, Factory::create4013},
{4040, Factory::create4040}};
};
但是我有以下错误:
includes/Factory.hpp:24:68: error: could not convert ‘{{4001, ((Factory*)this)->Factory::create4001}, {4013, ((Factory*)this)->Factory::create4013}, {4040, ((Factory*)this)->Factory::create4040}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<int, nts::IComponent* (*)(const std::__cxx11::basic_string<char>&)>’
{4040, Factory::create4040}};
知道吗?
指向
(非静态)成员函数的指针的typedef
如下所示:
typedef nts::IComponent *(Factory::*createFunction)(const std::string &value) const;
// ^^^^^^^ ^^^^^
// nested name specifier missing const
优选形式:
using createFunction = nts::IComponent *(Factory::*)(const std::string &value) const;
地图初始化:
std::map<int, createFunction> map = {{4001, &Factory::create4001},
{4013, &Factory::create4013},
{4040, &Factory::create4040}};
// ^
// compiler would think you're trying to call
// a static function without an argument list
相关文章:
- 如何创建对象函数指针C++映射?
- c++:复制、删除和运算符=在原始指针映射中
- 尝试通过指针映射访问类成员
- 使用函数指针映射时的 C++ 调用函数
- 将新元素推送到函数指针映射中 "inline" ?
- 如何在C 11中创建指针映射以静态成员功能
- 方法指针映射,编译器说他们不接受任何参数
- 从指针映射调用函数,指向具有可变数量参数的函数
- 如何使用boost::与函数指针映射正确绑定
- std::按值或指针映射操作
- 将函数指针映射到 std::string
- 指向指针向量的指针映射的错误
- 使用C++中的函数指针映射枚举键和值
- 创建指向成员函数的指针映射
- 无法从指向多态类的指针映射获取有效的指针
- C++ - 通过 getter 函数在单独的类中从对象指针映射访问成员函数
- 指向成员函数的指针映射:无法将 void (*) 转换为 void (对象::*)
- std::基类指针映射
- 为对象指针映射编写函数模板时出现问题
- 指针映射的数组下标运算符