C*******l 发帖数: 105 | 1 C++中有些code把函数返回值定义成const,这样做的好处是什么?我的理解是函数返
回值已经是rvalue了,已经不能改变了,但为什么还要在函数定义时写上const,这是
不是多此一举? | k**f 发帖数: 372 | 2
Usually const is used along with pointer or reference in the return type.
The returned value can only be assigned to a const pointer or bonded to a
const reference. If the underlying type is a class (struct), only the const
method can be called using such const pointers and references.
Otherwise, if you return a non-const pointer or reference, you can change
the object through the pointer or reference.
【在 C*******l 的大作中提到】 : C++中有些code把函数返回值定义成const,这样做的好处是什么?我的理解是函数返 : 回值已经是rvalue了,已经不能改变了,但为什么还要在函数定义时写上const,这是 : 不是多此一举?
| C*******l 发帖数: 105 | 3 right, thanks
const
【在 k**f 的大作中提到】 : : Usually const is used along with pointer or reference in the return type. : The returned value can only be assigned to a const pointer or bonded to a : const reference. If the underlying type is a class (struct), only the const : method can be called using such const pointers and references. : Otherwise, if you return a non-const pointer or reference, you can change : the object through the pointer or reference.
|
|