定义未指定名称空间的函数指针
Defining a function pointer of an unspecified namespace
我正在为一款与朋友合作的游戏制作一个简单的开发者主机。我正在将函数绑定到控制台,所以我有一个std::map包含一个字符串来保存我们将在控制台调用它的名称,以及我自己定义的MFP类型,这是一个函数指针,返回sf:: string(我们使用SFML, sf是SFML名称空间),并接受sf:: string作为参数。所有控制台函数都接受一个sf::String参数,并返回一个sf::String参数。
下面是有问题的代码(不是全部代码):
#include <SFML/System/String.hpp>
using namespace sf;
#include <map>
#include <string>
using namespace std;
class CConsole
{
public:
typedef sf::String (*MFP)(sf::String value); //function pointer type
void bindFunction(string name, MFP func); //binds a function
void unbindFunction(string name); //unbinds desired function
private:
map <string, MFP> functions;
}
现在这一切都很好,只要我们试图绑定到控制台的函数是全局命名空间。但这行不通。不断地为我们想要绑定到控制台的每个嵌套函数创建全局包装器函数,这将是效率太低了。
是否有可能使'MFP'接受所有名称空间的函数指针?例如,让下面的代码完美地工作?
#include "console.h" //code shown above
//Let's also pretend CConsole has an sf::String(sf::String value) method called consoleFunc that returns "Hello from the CConsole namespace!"
sf::String globalFunc(sf::String value)
{
return "Hello from the global namespace!";
}
int main()
{
CConsole console;
console->bindFunction("global", globalFunc);
console->bindFunction("CConsole", CConsole::consoleFunc);
return 0;
}
在您的示例中,您可以在任何非成员函数或任何类的静态成员函数上调用bindFunction
。不能使用非静态成员bindFunction
,因为它们具有不同的类型。
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- c++r值引用应用于函数指针
- 模板函数指针和lambda
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 带有类的函数指针
- () 函子后面的括号,而不是函数指针?
- 全局作用域中函数指针的赋值
- 使用"Task"函数指针队列定义作业管理器
- 将成员函数指针作为参数传递给模板方法
- 如何创建对象函数指针C++映射?
- 匹配函数指针作为模板参数?
- 通过函数指针定义类范围之外的方法
- 存储在类中的函数指针
- C++从函数指针数组调用函数
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 整数键映射到头文件中的成员函数指针
- 从类成员函数到类 C 函数指针的转换
- 如何将内联匿名函数分配给C++函数指针
- 将字符缓冲区强制转换为函数指针