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"限定符创建静态成员函数
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
