保存类模板化函数指针内映射
c++ save class templated function pointer inside map
我有一个小问题,我无法理解。我有一个函数,看起来像这样:
template <typename T>
std::unique_ptr<Environment>& CreateEnvironment(sf::Vector2f& _position, bool _addToStatic = false);
这是函数指针typepedef
typedef std::unique_ptr<Environment>& (WorldEditor::*CreateEnvironmentPtr)(sf::Vector2f&, bool);
std::map<std::string,CreateEnvironmentPtr> listEnv;
我试着简单地这样做:
listEnv["test"] = &CreateEnvironment<Coin>(sf::Vector2f(200,200), false);
,我得到以下错误:
error C2440: '=' : cannot convert from 'std::unique_ptr<_Ty> *' to 'std::unique_ptr<_Ty> &(__thiscall WorldEditor::* )(sf::Vector2f &,bool)'
我明白错误在说什么,但我不知道如何解决它。另外,当我指向函数的地址时,为什么它甚至关心返回类型?
致以最亲切的问候尼洛
使用std::function
通常可以更好地解决此类问题
std::map<std::string, std::function<void()> listEnv;
listEnv.emplace("test", [] {
CreateEnvironment<Coin>(sf::Vector2f(200,200), false);
});
调用:
listEnv.at("test")->second();
根据你的帖子,我不确定你是否试图在CreateEnvironment
类内部或外部创建成员函数指针和映射,所以我将解决我认为是指向单独对象的成员函数的更困难的问题。
我这样简化了你的类:
环境struct Environment
{
int i = 1;
};
硬币struct Coin
{
int k = 0;
};
WorldEditor
struct WorldEditor
{
template <typename T>
std::unique_ptr<Environment> CreateEnvironment(int& _j, bool _addToStatic = false)
{
return std::make_unique<Environment>();
}
};
解决方案:映射对象的成员fn指针,然后稍后调用它(我将使用c++ 11/14语法在我的回答)
//declare a pointer to member function in WorldEditor
using CreateEnvironmentPtr = std::unique_ptr<Environment> (WorldEditor::*)(int&, bool);
//declare an object of type WorldEditor, because member function pointers need a "this" pointer
WorldEditor myWorldEditor;
int myInt = 42;
//map a string to the CreateEnvironment<Coin> function
std::map<std::string, CreateEnvironmentPtr> listEnv;
listEnv["test"] = &WorldEditor::CreateEnvironment<Coin>;
// call the member function pointer using the instance I created, as well as
// the mapped function
(myWorldEditor.*listEnv["test"])(myInt, false);
// (printing member value to cout to show it worked)
std::cout << (myWorldEditor.*listEnv["test"])(myInt, false)->i << std::endl; // prints 1
现场演示
方案2:使用std::bind和std::function
在创建map的表项时,可能已经知道成员函数调用的形参。使用std::bind
和std::function
将帮助我们实现这一点(类似于Richard Hodges的解决方案):
// now our "function pointer" is really just a std::function that takes no arguments
using CreateEnvironmentPtr = std::function<std::unique_ptr<Environment>(void)>;
//declare an object of type WorldEditor, because member function pointers need a "this" pointer
WorldEditor myWorldEditor;
int myInt = 42;
//map a string to that function pointer
//ensure it gets called with the right args
// by using std::bind (which will also make the arg list appear the be void at call time)
// note that std::bind needs an instance of the class immediately after
// listing the function it should be binding
// only afterwards will we then pass the int& and bool
std::map<std::string, CreateEnvironmentPtr> listEnv;
listEnv["test"] = std::bind(&WorldEditor::CreateEnvironment<Coin>, &myWorldEditor, myInt, false);
// the mapped function
listEnv["test"]()->i;
// (printing resulting unique_ptr<Environment>'s member to cout to show it worked)
std::cout << listEnv["test"]()->i << std::endl; // prints 1
Live Demo 2
相关文章:
- 如何创建对象函数指针C++映射?
- c++:复制、删除和运算符=在原始指针映射中
- 尝试通过指针映射访问类成员
- 使用函数指针映射时的 C++ 调用函数
- 将新元素推送到函数指针映射中 "inline" ?
- 如何在C 11中创建指针映射以静态成员功能
- 方法指针映射,编译器说他们不接受任何参数
- 从指针映射调用函数,指向具有可变数量参数的函数
- 如何使用boost::与函数指针映射正确绑定
- std::按值或指针映射操作
- 将函数指针映射到 std::string
- 指向指针向量的指针映射的错误
- 使用C++中的函数指针映射枚举键和值
- 创建指向成员函数的指针映射
- 无法从指向多态类的指针映射获取有效的指针
- C++ - 通过 getter 函数在单独的类中从对象指针映射访问成员函数
- 指向成员函数的指针映射:无法将 void (*) 转换为 void (对象::*)
- std::基类指针映射
- 为对象指针映射编写函数模板时出现问题
- 指针映射的数组下标运算符