Why do function pointers all have the same value?

前端 未结 4 541
难免孤独
难免孤独 2021-01-23 05:30

For example:

using namespace std;
#include 

void funcOne() {
}

void funcTwo( int x ) {
}

int main() {

  void (*ptrOne)() = funcOne;
  cout &l         


        
4条回答
  •  死守一世寂寞
    2021-01-23 05:46

    Try this instead:

    void (*ptrOne)() = funcOne;
    cout << reinterpret_cast(ptrOne) << endl;
    
    void (*ptrTwo)( int x ) = funcTwo;
    cout << reinterpret_cast(ptrTwo) << endl;
    
    int (*ptrMain)() = main;
    cout << reinterpret_cast(ptrMain) << endl;
    

    I think normally, function overloading rules mean that the version of << being called is operator<<(bool), which means:

    cout << ptrOne << endl;
    

    Gets transformed into:

    cout << (ptrOne != NULL) << endl;
    

    Which is the same as:

    cout << true << endl;
    

提交回复
热议问题