Default value of a function pointer in C++

后端 未结 5 1362
说谎
说谎 2020-12-18 05:18

What is the default value of a function pointer in C++? (Apparently it can\'t be NULL, so what is it?)

How is this program supposed to behave and why?

5条回答
  •  一个人的身影
    2020-12-18 05:42

    In C++ (and C), pointers (regardless of type) do not have a default value per se; they take what ever happens to be in memory at the time. However, they do have a default initialised value of NULL.

    Default Initialisation

    When you don't explicitly define a constructor, C++ will call the default initialiser on each member variable, which will initialise pointers to 0. However, if you define a constructor, but do not set the value for a pointer, it does not have a default value. The behaviour is the same for integers, floats and doubles.

    Aside

    int main()
    {
        S s = S();
        s.f();   // <-- This is calling `f`, not getting the pointer value.
    }
    

提交回复
热议问题