C++ - Why static member function can#39;t be created with #39;const#39; qualifier(C++ - 为什么不能使用“const限定符创建静态成员函数)
问题描述
今天我遇到了一个问题.我需要一个 static 成员函数,const 不是必须的,而是更好的.但是,我的努力没有成功.有人能说出原因或方式吗?
Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?
推荐答案
当您将 const 限定符应用于非静态成员函数时,它会影响 this 指针.对于 C 类的 const 限定成员函数,this 指针的类型是 C const*,而对于不是const 限定,this 指针的类型为 C*.
When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C, the this pointer is of type C const*, whereas for a member function that is not const-qualified, the this pointer is of type C*.
静态成员函数没有 this 指针(此类函数不会在类的特定实例上调用),因此静态成员函数的 const 限定没有任何意义.
A static member function does not have a this pointer (such a function is not called on a particular instance of a class), so const qualification of a static member function doesn't make any sense.
这篇关于C++ - 为什么不能使用“const"限定符创建静态成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ - 为什么不能使用“const"限定符创建静态成员函数
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
