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

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
vector<const SomeClass*> vec;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.