I have a struct containing a pointer, and I want to pass it to a function such that the function can't modify the contents of the pointer. For example,
1 2 3 4
struct mystruct{
int v;
int* p;
};
Certainly I can cast to a "const mystruct", eg
1 2 3
mystruct a;
const mystruct& b=a;
b.v=3; // not allowed!
and this prevents modification of b.v and b.p, but not *b.p. How do I cast it to make everything const? ie how do I cast it to something like this?
class T {
int n_;
public:
int getN() const { return n_; }
void setN( int n ) { n_ = n; }
};
T a;
cout << a.getN(); // ok
a.setN( 1 ); // ok
const T b;
cout << b.getN(); // ok
b.setN( 1 ); // error: discards qualifiers or something like that...