What does const mean following a function/method signature?(遵循函数/方法签名的 const 是什么意思?)
问题描述
根据 MSDN:当跟随成员函数的参数列表时,const 关键字指定该函数不会修改调用它的对象."
According to MSDN: "When following a member function's parameter list, the const keyword specifies that the function does not modify the object for which it is invoked."
有人可以澄清一下吗?这是否意味着该函数不能修改任何对象的成员?
Could someone clarify this a bit? Does it mean that the function cannot modify any of the object's members?
bool AnalogClockPlugin::isInitialized() const
{
return initialized;
}
推荐答案
表示该方法不修改成员变量(声明为mutable
的成员除外),所以可以调用类的常量实例.
It means that the method do not modify member variables (except for the members declared as mutable
), so it can be called on constant instances of the class.
class A
{
public:
int foo() { return 42; }
int bar() const { return 42; }
};
void test(const A& a)
{
// Will fail
a.foo();
// Will work
a.bar();
}
这篇关于遵循函数/方法签名的 const 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:遵循函数/方法签名的 const 是什么意思?


基础教程推荐
- C语言基础全局变量与局部变量教程详解 2022-12-31
- C++详细实现完整图书管理功能 2023-04-04
- C/C++编程中const的使用详解 2023-03-26
- C语言 structural body结构体详解用法 2022-12-06
- 如何C++使用模板特化功能 2023-03-05
- 详解c# Emit技术 2023-03-25
- C++中的atoi 函数简介 2023-01-05
- C利用语言实现数据结构之队列 2022-11-22
- 一文带你了解C++中的字符替换方法 2023-07-20
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26