指向const成员函数typedef的指针 [英] pointer to const member function typedef
问题描述
我知道可以单独创建一个成员函数的指针,像这样
I know it's possible to separate to create a pointer to member function like this
struct K { void func() {} };
typedef void FuncType();
typedef FuncType K::* MemFuncType;
MemFuncType pF = &K::func;
有类似的方法来构造一个指向const函数的指针吗?我试着添加const在各个地方没有成功。我已经玩过gcc一些,如果你在像
Is there similar way to construct a pointer to a const function? I've tried adding const in various places with no success. I've played around with gcc some and if you do template deduction on something like
template <typename Sig, typename Klass>
void deduce(Sig Klass::*);
它将显示Sig作为一个函数签名与const刚刚粘在末端。如果在代码中这样做,它会抱怨你不能有一个函数类型的限定符。
It will show Sig with as a function signature with const just tacked on the end. If to do this in code it will complain that you can't have qualifiers on a function type. Seems like it should be possible somehow because the deduction works.
推荐答案
您希望这样:
typedef void (K::*MemFuncType)() const;
如果您仍然要 MemFuncType
FuncType
,您需要更改 FuncType
:
If you want to still base MemFuncType
on FuncType
, you need to change FuncType
:
typedef void FuncType() const;
typedef FuncType K::* MemFuncType;
这篇关于指向const成员函数typedef的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!