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
The type of the variable
pisint (C::*const) (const int&) const, you can define it without a typedef as:Your rule of thumb is: to make the object/type that you’re defining const, put the
constkeyword next to the name of the object/type. So you could also do: