1

I guess this is a very basic question.

when I declare a vector of const pointers, like this:

vector<const SomeClass*> vec;

Is it a declaration that the pointers are const or the objects that are pointed to by the array element?

thanks

Day_Dreamer
  • 3,311
  • 7
  • 34
  • 61
  • I think you might find this particular page of the C++ FAQ useful. http://www.parashift.com/c++-faq/const-correctness.html – James Matta Dec 31 '14 at 19:49
  • possible duplicate of [what is the difference between const int\*, const int \* const, int const \*](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const) – OMGtechy Dec 31 '14 at 20:45

2 Answers2

2
vector<const SomeClass*> vec;

It's declaring a vector containing pointers to const objects of type SomeClass.

ravi
  • 10,994
  • 1
  • 18
  • 36
2

There are two places the const could go:

T* p1;                  // non-const pointer, non-const object
T const* p2;            // non-const pointer, const object
T* const p3;            // const pointer, non-const object
T const* const p4;      // const pointer, const object

Just read from right-to-left. For this reason, it becomes clearer if you write types as T const instead of const T (although in my code personally I still prefer const T).

You are specifically constructing a vector of pointers to const objects. Note that you could not create a vector of const pointers, since a vector requires its elements to be copyable (or, in C++11, at least movable) and a const pointer is neither.

Barry
  • 286,269
  • 29
  • 621
  • 977