【发布时间】:2012-01-29 11:13:15
【问题描述】:
如果代码中有类似下面的内容:
func(const base& obj)
const 语义是什么意思?什么是不变的? obj 是 const reference to a non-const object 还是 non-const reference to a const object?
【问题讨论】:
标签: c++ reference constants semantics
如果代码中有类似下面的内容:
func(const base& obj)
const 语义是什么意思?什么是不变的? obj 是 const reference to a non-const object 还是 non-const reference to a const object?
【问题讨论】:
标签: c++ reference constants semantics
正如其他答案所说,obj 是对const base 对象的引用。但是,这并不意味着它所引用的对象恰好具有base 类型,或者它所引用的对象是const,只是func 不能修改obj 通过那个参考。例如:
struct derived : base { ... };
derived d;
func(d);
是合法的,并且:
bool other_func(const base& b, other_object& o) {
base b_copy = b;
o.foo();
return b_copy == b;
}
如果o 具有对b(或其中的某些内容)的内部非常量引用并且o.foo() 修改b,则可能返回false。这对诸如
std::string::operator=(const std::string& other);
幼稚的实现可能会对my_str = my_str 做错事。
【讨论】:
obj 是对 const base 的引用,因此这意味着您不允许更改引用的对象。可以写成
func(const base& obj)
或
func(base const & obj)
使用从右到左的规则来读取这样的声明类型,对于这个简单的例子,只需从右边读取它。更多信息在这里:
【讨论】:
如果没有 const,您将无法向该函数发送 const 对象。所以添加 const 总是积极的。特别是当您为许多用户创建功能时。经典示例是 setter 函数。
x->setXsth(sth& obj) // works only with non-const object.
x->setXsth(const sth& obj) //works with const object and non-const.
【讨论】:
有些不请自来的答案/观点:const 修饰符修改其左侧的任何内容,除了您正在使用的一种构造(在这种情况下,它会立即修改任何内容正确的)。我发现总是将 const 放在我要修改的任何内容的右侧更容易,并从右到左阅读语句。也许这不是最好的做事方式,但它可以帮助我保持直截了当。
例子:
// these two statements are equivalent
const int x = 5; // special case usage
int const x = 5;
// using the LHS syntax makes multiple consts easier to understand
int const y = 6;
int const * const x = &y; // x is a const pointer to const int
// const can apply to pointers but not to references
int const & const z = y; // redundant, references are always const
【讨论】:
obj 是在参数中传递给func() 的对象的常量引用(无论对象是 const 还是非 const)
如果你写:func(B);
这意味着你不能在函数func()中改变B的内容
(其中func(const base& obj))
【讨论】:
它被称为常量引用。您对已传递的数据具有“引用访问权限”,但您无法对其进行修改。
【讨论】:
没有“非常量”引用这样的东西,也就是说,引用总是绑定到同一个对象,并且没有办法改变它。 "const type&" 表示引用 const 类型。
【讨论】:
const base &obj也可以用作base const &obj?
obj 是一个对const 对象的引用。
没有“非常量引用”这样的东西,因为引用在创建后无法更改为引用其他内容。
【讨论】:
func的参数传递?
func() 中,对象的使用仅限于那些对const 对象有效的操作。所以调用者知道调用func(obj)不会改变obj。