I am tracking three color points – red, green and blue. I have currentFrames class, which holds a couple of frames – frame from camera and some auxiliary ones – and some methods to manipulate/extract some info from frames. Let’s say I am able to locate centroid of each point, and then I want to call specific function based on position of this centroid. Whole frame is divided into grid and each segment of the grid will have function assigned to it – that function will be called when point is detected in this particular segment.
My idea is to create multi-dimensional array of pointers to member functions:
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
MyFuncPointerType functions[NUMBER_OF_POINTS][NUMBER_OF_SEGMENTS];
and then assign addresses of functions to this like:
functions[0][0] = ¤tFrames::something;
When, for example, first point will be located in second segment of the grid, then I will call proper function (and this will be called from inside currentFrames class – that’s why I am passing *this as a object to CALL_MEMBER_FN:
CALL_MEMBER_FN(*this, functions[0][1])();
I have started with not so complicated example – I want to call different functions for each point, but the same for each segment (“in scope of point”):
class currentFrames
{
public:
void someMethod();
private:
void calculateVelocity();
void redPointDetected();
void redPointGone();
typedef void (currentFrames::*MyFuncPointerType)();
MyFuncPointerType functions[];
}
Then, in constructor of currentFrames I do the following:
currentFrames::currentFrames()
{
this->functions[0] = ¤tFrames::calculateVelocity;
this->functions[1] = ¤tFrames::redPointDetected;
}
Then, in someMethod in currentFrames class I try to call member function:
void currentFrames::someMethod()
{
CALL_MEMBER_FN(*this, this->functions[0])();
}
But this gives me segfault. I tried to define CALL_MEMBER_FN as ((ptrToObject)->*(ptrToMember)), but this also gives segfault.
How this should be done? Is there a better way to achive what I want?
You forgot to give the length of array. This will make it equvilent to
MyFuncPointerType *functions. The pointer is initialized to some garbage address (probably 0) and causes segment when accessingthis->functions[0]in the constructor. This has nothing to do with CALL_MEMBER_FN.Change this line to
and it should work. (Demo: http://ideone.com/q4D3e)