Cast vector<T> to vector<const T>

Posted by user345386 on Stack Overflow See other posts from Stack Overflow or by user345386
Published on 2010-05-19T18:54:58Z Indexed on 2010/05/19 19:00 UTC
Read the original article Hit count: 171

Filed under:
|

I have a member variable of type vector (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*

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!

© Stack Overflow or respective owner

Related posts about c++

Related posts about const