How to declare a member function const pointer while point to const address

廉价感情. 提交于 2019-12-11 11:28:45

问题


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.

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.

This is not what I'm asking about.


回答1:


Member function pointers can't be dereferenced to modify the pointee, so only one const is needed:

RetType (Class::* const ptr)(Arg1Type, ..., ArgNType) = ...;

With typedefs:

typedef RetType (Class::* PtrTypedefName)(Arg1Type, ..., ArgNType);
const PtrTypedefName ptr = ...;

Hope this helps!



来源:https://stackoverflow.com/questions/20699916/how-to-declare-a-member-function-const-pointer-while-point-to-const-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!