Original Post
I have a test class with a function pointer that I want to dynamically change to point to another implementation. I don't use function pointers that often so I'm a little confused on the syntax. Right now I'm calling a regular function to call the function pointer but that's not what I want. I want to be able to call the function directly and have it look like a normal function. If you look at my main function and class source code you'll see what I'm doing. And here is my main:
// MyClass.h ////////////////////////////////////////////
#pragma once
class MyClass
{
public:
static bool m_IsFirst;
int (MyClass::*FunctionPointer)(int a);
int FirstFunction(int a) { return a*2; }
int SecondFunction(int a) { return a*4; }
int CallFunctionPointer(int a) { return (this->*FunctionPointer)(a); }
MyClass(void);
~MyClass(void){};
};
// MyClass.cpp //////////////////////////////////////////
#include "MyClass.h"
bool MyClass::m_IsFirst = false;
MyClass::MyClass(void)
{
if(m_IsFirst) {
FunctionPointer=MyClass::FirstFunction;
} else {
FunctionPointer=MyClass::SecondFunction;
}
}
#include "stdafx.h"
#include "MyClass.h"
#pragma comment(lib, "Test.lib")
int _tmain(int argc, _TCHAR* argv[])
{
MyClass::m_IsFirst = true;
MyClass* pTest = new MyClass();
// LOOK HERE: This works but I have to call another function to call my function pointer.
int a = pTest->CallFunctionPointer(5);
// I want something like this:
// int a = pTest->FunctionPointer(5); // See how it's called directly
return 0;
}