Why do function pointers all have the same value?

前端 未结 4 532
难免孤独
难免孤独 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条回答
  •  Happy的楠姐
    2021-01-23 06:07

    Operator overloading in C++ adds all sorts of nasty complexities. (It lets you do awesome stuff too—but sometimes it's just a headache.)

    As explained in the other answers, C++ is doing some automatic type coercion on your function pointers. If you just use the good ol' C-style printf you should get the results you're expecting:

    #include 
    
    // ...
    
    printf("func1: %p\nfunc2: %p\n", funcOne, funcTwo);
    

提交回复
热议问题