Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const vector implies const elements?

Tags:

c++

constants

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

In the code below,

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

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

Why?

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

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); } // () 
like image 408
cibercitizen1 Avatar asked Nov 21 '14 16:11

cibercitizen1


People also ask

What does const vector mean?

A const vector will return a const reference to its elements via the [] operator . In the first case, you cannot change the value of a const int&. In the second case, you cannot change the value of a reference to a constant pointer, but you can change the value the pointer is pointed to. – Julian.

Can you modify const vector in C++?

A const iterator points to an element of constant type which means the element which is being pointed to by a const_iterator can't be modified.

Can a const vector be modified?

If the vector itself is declared const (as in const std::vector<T*>), then you can't modify the vector, but you can modify the objects. If the pointers are declared const (as in std::vector<const T*>), then you can modify the vector, but not the objects.

Can you add to a const vector?

You can't put items into a const vector, the vectors state is the items it holds, and adding items to the vector modifies that state. If you want to append to a vector you must take in a non const ref.


2 Answers

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

like image 77
lisyarus Avatar answered Oct 08 '22 02:10

lisyarus


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.

The second version, on the other hand, only reads from the vector

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

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

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

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.

like image 43
Sergey Kalinichenko Avatar answered Oct 08 '22 01:10

Sergey Kalinichenko