如何在指向const地址时声明成员函数const指针 [英] How to declare a member function const pointer while point to const address
问题描述
我的意思是类似const char* const p;
或char const * const p;
的东西.此处的p
表示指针指向const char,而指针本身也是const.
因此,不会遵守*p = 'a';
或char c = 'c'; p = &c;
.
What I mean is something like const char* const p;
or char const * const p;
. The p
here stand for a pointer points to a const char while the pointer itself is also const.
So *p = 'a';
or char c = 'c'; p = &c;
won't be complied.
请有人告诉我如何声明一个指向成员函数的指针,该成员函数指向的对象本身就是const,带有和不带有typedef
.
Please someone tell me how to declare a pointer points to a member function, both what it points to and itself is const, with and without typedef
.
不要只是出于好奇而在实践中使用.
Not use in practice just curious about.
推荐答案
不能取消引用成员函数指针来修改指针,因此只需要一个const:
Member function pointers can't be dereferenced to modify the pointee, so only one const is needed:
RetType (Class::* const ptr)(Arg1Type, ..., ArgNType) = ...;
使用typedefs:
With typedefs:
typedef RetType (Class::* PtrTypedefName)(Arg1Type, ..., ArgNType);
const PtrTypedefName ptr = ...;
希望这会有所帮助!
这篇关于如何在指向const地址时声明成员函数const指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!