对函数指针数组声明有疑问

Doubt about array of function pointers declaration

本文关键字:声明 有疑问 数组 指针 函数      更新时间:2023-10-16

我已经设法用这段代码制作了一个"指针到成员函数数组",它工作得很好…

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);
    }
}

但是,如果我将数组声明放在函数之外,就像下面的代码一样,我会在Visual Studio中得到缓冲区溢出访问冲突,尽管代码编译

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);
    }
}

有人知道如何解释为什么我需要保持它在函数范围内吗?

您没有使用i索引到数组。

其次,使用std::beginstd::end,它们已经为你处理好了。

最后,ptmf基本上毫无价值。使用std::function

编辑:

可能发生的事情是你搞砸了大小计算,而VS只碰巧注意到它是静态的。在两个版本中,它可能同样被破坏了。这就是你的UB。

再次编辑:您将类型为string的对象传递给printf ?如果这是不是一个类型定义为const char*,那么你好UB。如果

string result = (helper.*reportFunArray[i])();
printf("%s", result);

我假设string实际上是std::string ?你是using namespace std;吗?

在这种情况下,printf("%s", result);很可能崩溃,因为它会将天知道的东西解释为c风格字符串的第一个字符。下面的工作吗?

string result = (helper.*reportFunArray[i])();
std::cout << result;