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"限定符创建静态成员函数


- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07
- C++ 协变模板 2021-01-01