请帮我理解以下签名:
err_type funcName(const Type& buffer) const;
Run Code Online (Sandbox Code Playgroud)
所以对于第一个const,这是否意味着Type的内容不能改变或者引用不能改变?
其次,第二个const是什么意思?我甚至都没有暗示.
在此先感谢,jbu
R S*_*hko 18
第二个const意味着可以在const对象上调用该方法.
考虑这个例子:
class foo
{
public:
void const_method() const;
void nonconst_method();
};
void doit()
{
const foo f;
f.const_method(); // this is okay
f.nonconst_method(); // the compiler will not allow this
}
Run Code Online (Sandbox Code Playgroud)
此外,不允许const方法更改对象的任何成员(除非该成员被特别标记为可变):
class foo
{
public:
void const_method() const;
private:
int r;
mutable int m;
};
void foo::const_method() const
{
m = 0; // this is okay as m is marked mutable
r = 0; // the compiler will not allow this
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1113 次 |
| 最近记录: |