字符串和成员函数指针的映射
C++ Map of string and member function pointer
嘿,所以我正在制作一个映射字符串作为关键和成员函数指针作为值。我似乎不知道如何添加到地图上,这似乎不工作。
#include <iostream>
#include <map>
using namespace std;
typedef string(Test::*myFunc)(string);
typedef map<string, myFunc> MyMap;
class Test
{
private:
MyMap myMap;
public:
Test(void);
string TestFunc(string input);
};
#include "Test.h"
Test::Test(void)
{
myMap.insert("test", &TestFunc);
myMap["test"] = &TestFunc;
}
string Test::TestFunc(string input)
{
}
value_type
参见std::map::insert
和std::map
myMap.insert(std::map<std::string, myFunc>::value_type("test", &Test::TestFunc));
和operator[]
myMap["test"] = &Test::TestFunc;
不能在没有对象的情况下使用指向成员函数的指针。可以将指向成员函数的指针用于Test
Test t;
myFunc f = myMap["test"];
std::string s = (t.*f)("Hello, world!");
或指向类型Test
Test *p = new Test();
myFunc f = myMap["test"];
std::string s = (p->*f)("Hello, world!");
参见c++ FAQ -指向成员函数的指针
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- 智能指针作为无序映射键,并通过引用进行比较
- C++:映射结构中的结构指针
- 如何创建对象函数指针C++映射?
- 整数键映射到头文件中的成员函数指针
- C++在一个映射中存储不同的指针类型(并处理销毁)
- 添加要映射的对象的唯一指针
- 通过指针或引用将映射传递给函数?
- 在映射中存储模板类函数指针
- c++:复制、删除和运算符=在原始指针映射中
- 将唯一指针插入std::映射
- 在循环(C++)中用新指针填充映射
- C++:如何初始化包含具有给定键的指针列表的映射
- 如何将带指针的结构插入到映射中
- 重构MFC消息映射以包括完全限定的成员函数指针
- C 正确的方法来初始化和处理映射指针
- 定义基于模板的映射指针,以便在验证范围时将 int 解析为枚举
- C++中的静态无序映射指针初始化
- 动态分配映射指针值
- 如何在c++中维护映射指针的向量