【发布时间】:2023-04-02 23:26:01
【问题描述】:
给定一个班级
class C {
public:
int f (const int& n) const { return 2*n; }
int g (const int& n) const { return 3*n; }
};
我们可以像这样定义一个函数指针p 到C::f。
int (C::*p) (const int&) const (&C::f);
p 的定义可以使用typedef 进行拆分:
typedef int (C::*Cfp_t) (const int&) const;
Cfp_t p (&C::f);
为了确保p 不会改变(例如p = &C::g;),我们可以这样做:
const Cfp_t p (&C::f);
现在,在这种情况下,p 的类型是什么?我们如何在不使用 typedef 的情况下完成p 的最后定义?
我知道typeid (p).name () 无法区分最外层的 const,因为它产生了
int (__thiscall C::*)(int const &)const
【问题讨论】:
标签: c++ member-function-pointers