I've managed to make a "Pointer-to-member function array" with this code, and it works fine...
typedef string (MyReportHelper::*reportFunctions)();
MyReportHelper helper;
void internalPointersTest(){
reportFunctions reportFunArray[] = {
&MyReportHelper::getVersion,
&MyReportHelper::getModel,
&MyReportHelper::getUsername,
};
int arrSize = sizeof(reportFunArray)/sizeof(reportFunArray[0]);
for(int i = 0; i < arrSize; i++){
string result = (helper.*reportFunArray[i])();
printf("%s", result);
}
}
But if I put the array declaration outside my function, like the following code, I get a buffer overrun or access violation in Visual Studio, though the code compiles.
typedef string (MyReportHelper::*reportFunctions)();
MyReportHelper helper;
reportFunctions reportFunArray[] =
{
&MyReportHelper::getVersion,
&MyReportHelper::getModel,
&MyReportHelper::getUsername,
};
void internalPointersTest(){
int arrSize = sizeof(reportFunArray)/sizeof(reportFunArray[0]);
for(int i = 0; i < arrSize; i++)
{
//next line will fail
string result = (helper.*reportFunArray[i])();
printf("%s", result);
}
}
Does anyone know how to explain why I need to keep it inside the function scope?
helpercome from? Also, can we see the definitions ofgetVersion,getModelandgetUsername?helperdeclarationi < arrSizewithi < 3?