【发布时间】:2014-06-26 08:47:27
【问题描述】:
如何将指向静态函数的指针作为参数从静态函数内部传递给另一个静态函数,它们都在同一个类中?我正在使用 VisualStudio 2010。 我的代码大致是这样的:
//SomeClass.h
class SomeClass
{
public:
static AnotherClass* doSomething(AnotherClass*, AnotherClass*);
static AnotherClass* doSomethingElse(AnotherClass*, AnotherClass*);
private:
typedef float (SomeClass::*SomeOperation)(float, float);
static AnotherClass* apply(AnotherClass*,
AnotherClass*,
SomeOperation);
static float SomeClass::operationA(float, float);
static float SomeClass::operationB(float, float);
};
//SomeClass.cpp
AnotherClass* SomeClass::doSomething(AnotherClass* a, AnotherClass* b)
{
return apply(a, b, &SomeClass::operationA);
}
AnotherClass* SomeClass::doSomethingElse(AnotherClass* a, AnotherClass* b)
{
return apply(a, b, &SomeClass::operationB);
}
AnotherClass* apply(AnotherClass* a,
AnotherClass* b,
SomeOperation op)
{
/* Some sanity checking and a lot of loop stuff which is the same
* for all operations a, b, c ... */
}
我尝试了不同的变体,但不断收到编译器错误,例如:
C2664“SomeClass::apply”:无法从“SomeClass::SomeOperation”中的“float (__cdecl *)(float, float)”转换参数 3。
有谁知道我做错了什么以及如何解决这个问题?
【问题讨论】:
-
我相信您想切换到C++11,这将为您提供anonymous functions 与closures 实现。考虑升级你的编译器:最近的GCC -e.g. 4.8 或 4.9- 很好地支持 C++11。
-
@Basile 即“使用 jQuery”?
-
@LightnessRacesinOrbit:或者“使用 Ocaml”或“使用 Common Lisp”......
标签: c++ static function-pointers argument-passing