What is the type of a constant method pointer?

c++, pointer-to-member

Solution

The type of the variable `p` is `int (C::*const) (const int&) const`, you can define it without a typedef as:

int (C::*const p) (const int&) const = &C::f;

Your rule of thumb is: to make the object/type that you're defining const, put the `const` keyword next to the name of the object/type. So you could also do:

typedef int (C::*const Cfp_t) (const int&) const;
Cfp_t p(&C::f);
p = &C::f; // error: assignment to const variable

Problem

Given a class ``` class C { public: int f (const int& n) const { return 2*n; } int g (const int& n) const { return 3*n; } }; ``` We can define a function pointer `p` to `C::f` like this. ``` int (C::*p) (const int&) const (&C::f); ``` The definition of `p` may be split up using a `typedef`: ``` typedef int (C::*Cfp_t) (const int&) const; Cfp_t p (&C::f); ``` To make sure `p` doesn't change (as by `p = &C::g;` for instance) we can do: ``` const Cfp_t p (&C::f); ``` Now, what is the type of `p` in this case? And how do we accomplish the last definition of `p` without using a typedef? I am aware that `typeid (p).name ()` cannot distinguish the outermost const as it yields ``` int (__thiscall C::*)(int const &)const ```

Original source