c ++函数指针没有改变

ali*_*i73 6 c++ pointers function

我已经定义了一些函数,我打印他们的地址是这样的:

#include<iostream>
#include <string>

using std::cout;

std::string func()
{
    return "hello world\n";

}

int func2(int n)
{
    if (n==0)
    {
        cout << func2 << std::endl;
        return 1;
    }

    cout << func2 << std::endl;

    return n + func2(n - 1);
}

//================================================
int main()
{
    int (*fun)(int) = func2;

    cout << fun;

    cout << std::endl << func2(3);
}
Run Code Online (Sandbox Code Playgroud)

当我打印函数的名称(地址)时,它们都1在我的编译器上打印(Mingw gcc 4.8).

它可以或它应该有所不同吗?

juz*_*lin 0

您没有打印地址,因为它现在已转换为布尔值。

但你可以这样做:

std::cout << reinterpret_cast<unsigned long long int *>(func2) << std::endl;
Run Code Online (Sandbox Code Playgroud)

现在您将获得实际地址。