C++: Initializing Struct and Setting Function Pointer

Viewed 1846

I am trying to initialize a struct with a function pointer, however I am having trouble doing so unless it is done with a global function. The following code works:

float tester(float v){
    return 2.0f*v;
}

struct MyClass::Example{
    typedef float(*MyFunc)(float);

    MyFunc NameOfFunc;

    float DoSomething(float a){
        return NameOfFunc(a);
    }
};

struct Example e;
e.MyFunc = tester;

My problem is whenever I try to do this with the tester function as a function in MyClass the code no longer works. In other words, I change the tester function to:

float MyClass::tester(float v){
    return 2.0f*v;
} 

Any and all help is appreciated (including links)! I have tried googling around for the problem but am unsure what to search (I've tried things such as "C++ struct function pointer initialization in class" to no avail)

2 Answers
Related