Reputation: 355
How to pass a pointer to a static function as argument to another static function from inside a static function, which are all inside the same class? Im using VisualStudio 2010. My code looks roughly like this:
//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 ... */
}
I have tried different variants but keep getting compiler errors like:
C2664 "SomeClass::apply": conversion of parameter 3 from 'float (__cdecl *)(float, float)' in 'SomeClass::SomeOperation' not possible.
Has anyone an idea what I'm doing wrong and how to fix this?
Upvotes: 1
Views: 116
Reputation: 30136
Change this:
private:
typedef float (SomeClass::*SomeOperation)(float, float);
To this:
public:
typedef float (*SomeOperation)(float, float);
Or you can simply declare typedef float (*SomeOperation)(float, float)
outside the class...
Upvotes: 2
Reputation: 18964
A static member function is just a function; you don't use pointer to member syntax for it.
So instead of
typedef float (SomeClass::*SomeOperation)(float, float);
You want
typedef float (*SomeOperation)(float, float);
And you can just pass operationA
rather than &SomeClass::operationA
.
Upvotes: 2
Reputation: 9383
Remove SomeClass::
from the typedef.
There are a variety of syntax errors in the example, but after fixing them and doing that, I could compile your code.
Upvotes: 0