C++ const 向量意味着 const 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27065617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:44:10  来源:igfitidea点击:

const vector implies const elements?

c++const

提问by cibercitizen1

Does const vector<A>mean that its elements are constas well?

是否const vector<A>意味着它的元素也是const如此?

In the code below,

在下面的代码中,

v[0].set (1234);in void g ( const vector<A> & v )produces the compiler error

v[0].set (1234);invoid g ( const vector<A> & v )产生编译器错误

const.cpp:28:3: error: member function 'set' not viable: 'this' argument has type 'const value_type' (aka 'const A'), but function is not marked const

const.cpp:28:3: 错误:成员函数“set”不可行:“this”参数的类型为“const value_type”(又名“const A”),但函数未标记为 const

Why?

为什么?

But (*v[0]).set(1234);in void h ( const vector<A *> & v )is OK for the compiler.

但是(*v[0]).set(1234);invoid h ( const vector<A *> & v )对编译器来说是可以的。

What's the difference between the versions?

版本之间有什么区别?

// ...........................................................
class A {
private:
  int a;
public:
  A (int a_) : a (a_) { }
  int get () const { return a; }
  void set (int a_) { a = a_; }
};

// ...........................................................
void g ( const vector<A> & v ) {
  cout << v[0].get();
  v[0].set (1234); 
} // ()

// ...........................................................
void h ( const vector<A *> & v ) {
  cout << (*v[0]).get();
  (*v[0]).set(1234);
} // ()

采纳答案by dasblinkenlight

The first version

第一个版本

v[0].set (1234); 

does not compile because it tries to change the vector's first element returned to it by reference. The compiler thinks it's a change because set(int)is not marked const.

不会编译,因为它试图更改通过引用返回给它的向量的第一个元素。编译器认为这是一个更改,因为set(int)没有标记const.

The second version, on the other hand, only readsfrom the vector

另一方面,第二个版本只从向量中读取

(*v[0]).set(1234);

and calls seton the result of the dereference of a constant reference to a pointer that it gets back.

并调用set对它返回的指针的常量引用的解引用的结果。

When you call v[0]on a constvector, you get back a constreference to A. When element type is a pointer, calling seton it is OK. You could change the second example to

当你调用v[0]一个const向量时,你会得到一个const对 的引用A。当元素类型是指针时,调用set它是可以的。您可以将第二个示例更改为

v[0]->set(1234);

and get the same result as before. This is because you get a reference to a pointer that is constant, but the item pointed to by that pointer is not constant.

并获得与以前相同的结果。这是因为您获得了对常量指针的引用,但该指针指向的项不是常量。

回答by lisyarus

Yes, a const vectorprovides access to its elements as if they were const, that is, it only gives you constreferences. In your second function, it's not the objects of type Athat are const, but pointersto them. A pointer being constdoes not mean that the object the pointer is pointing to is const. To declare a pointer-to-const, use the type A const *.

是的, aconst vector提供对其元素的访问,就好像它们是 一样const,也就是说,它只为您提供const引用。在您的第二个函数中,不是类型的对象Aconst,而是指向它们的指针。指针存在const并不意味着指针指向的对象是const。要声明一个指向常量的指针,请使用类型A const *

回答by Bill Lynch

So a const object can only call const methods. That is:

所以一个 const 对象只能调用 const 方法。那是:

class V {
  public:
    void foo() { ... }        // Can't be called
    void bar() const  { ... } // Can be called
};

So let's look at a vector's operator[]:

所以让我们看看向量的 operator[]

reference       operator[]( size_type pos );
const_reference operator[]( size_type pos ) const;

So when the vector object is const, it will return a const_reference.

因此,当向量对象为 const 时,它将返回 a const_reference

About: (*v[0]).set(1234);

关于: (*v[0]).set(1234);

Let's break this down:

让我们分解一下:

A * const & ptr = v[0];
A & val = *ptr;
val.set(1234);

Note that you have a constant pointer to variable data. So you can't change what is pointed at, but you can change the value that the pointer points at.

请注意,您有一个指向可变数据的常量指针。所以你不能改变指向的东西,但是你可以改变指针指向的值。