Cast vector<T> to vector<const T>

c++, constants

Solution

If you have a `const vector<int>` you cannot modify the container, nor can you modify any of the elements in the container. You don't need a `const vector<const int>` to achieve those semantics.

Problem

I have a member variable of type `vector<T>` (where is T is a custom class, but it could be int as well.) I have a function from which I want to return a pointer to this vector, but I don't want the caller to be able to change the vector or it's items. So I want the return type to be `const vector<const T>*` None of the casting methods I tried worked. The compiler keeps complaining that T is not compatible with const T. Here's some code that demonstrates the gist of what I'm trying to do; ``` vector<int> a; const vector<const int>* b = (const vector<const int>* ) (&a); ``` This code doesn't compile for me. Thanks in advance!

Original source

Related problems