相关疑难解决方法(0)

为什么函数指针都具有相同的值?

例如:

using namespace std;
#include <iostream>

void funcOne() {
}

void funcTwo( int x ) {
}

int main() {

  void (*ptrOne)() = funcOne;
  cout << ptrOne << endl;      //prints 1

  void (*ptrTwo)( int x ) = funcTwo;
  cout << ptrTwo << endl;      //prints 1

  int (*ptrMain)() = main;
  cout << ptrMain << endl;     //prints 1

}
Run Code Online (Sandbox Code Playgroud)

有谁知道这背后的原因?起初我以为这是因为函数不存在于内存中,因为我从不调用它们,因此它们永远不会被添加到堆栈中.但即使是指向main函数的指针的值也会输出1.

c++ function-pointers

2
推荐指数
1
解决办法
152
查看次数

标签 统计

c++ ×1

function-pointers ×1