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!
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.
In addition to James's answer about how to do it you should note that const int
is not a valid type to put into any standard container since it is not assignable.
On why a vector<T>
cannot be correctly converted to a vector<const T>
even if T
can be converted to const T
This is a common recurring problem in programming whether it is with constness or inheritance (a container of derived object cannot be converted to a container of base objects, even if the contained elements themselves can). The problem is that element by element each one of them can be converted, but the container itself cannot without breaking the type system.
If you were allowed to do vector< const T > &vr = my_vector_of_T
, then you would be allowed to add elements through vr
, and those elements would be constant by definition. But at the same time those same elements would be aliased in my_vector_of_T
as non-const elements and could be modified through that interface, breaking constness in the typesystem.
In the particular case of a vector<int>
being converted to a vector<const int>
, chances are that you would not notice really weird effects --besides adding an element to a vector<const int>
and seeing how the constant element changes in time, but still remember that given two related types T1
and T2
for which a relation exists, in most cases trying to apply the same relationship to containers of T1
and T2
will break the type system.
you can force conversion like this:
b = reinterpret_cast<const std::vector<const int>*>(&a);
but I do not think you should do this, since it is not guaranteed to work, only to compile
The compiler decides to block this. However, we know this is safe so maybe we can fool it:
const vector<const int>* b = (const vector<const int>* )(void *)(&a);