常量方法指针的类型是什么
What is the type of a constant method pointer?
给定一个类
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 ()
无法区分最外层的常量,因为它会产生
int (__thiscall C::*)(int const &)const
变量p
的类型是int (C::*const) (const int&) const
,您可以在没有typedef的情况下将其定义为:
int (C::*const p) (const int&) const = &C::f;
您的经验法则是:要使您定义的对象/类型为常量,请将const
关键字放在对象/类型的名称旁边。所以你也可以做:
typedef int (C::*const Cfp_t) (const int&) const;
Cfp_t p(&C::f);
p = &C::f; // error: assignment to const variable
相关文章:
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- 如何声明一个标准::提升直方图的向量?提升直方图的类型是什么?
- 类名后跟括号的类型是什么
- std::chrono::duration::count函数的实际结果类型是什么
- c++运算符重载-我实际返回的操作数类型是什么
- 运算符和返回类型是什么意思?
- 指向 2D 数组的指针的类型是什么?
- RSA_verify中预期的数据类型是什么,无法验证从 Java 生成的签名
- 共享_ptr中多功能数组的类型是什么
- 构造函数的类型是什么?
- 字符串中单个字符作为数组的数据类型是什么
- 该结构继承的类型是什么
- 如果我获得Integer和Double的产品,输出的数据类型是什么
- C 中被视为可可的类型是什么
- 当函数采用右值引用时,函数中该变量的类型是什么?
- STD :: SETW,STD :: SETFILL等的真实返回类型是什么?
- 模板类中引用的引用的类型是什么
- const自动参考绑定到(NULL)指针 - 实际类型是什么
- 包含通过引用捕获的 lambda 函数的"variable"的类型是什么?
- 通过插座发送的内容的数据类型是什么