This may sound silly.

I have a class that contains a vector of pointers to instantiated objects.
I want to return a refrence to that vector, but I don't want to be able to modify the vector through that refrence.
Now, if i return a const reference, the vector is const, but the objects pointed by within the vector can still be modified via the pointer.

As a short example:

lass VecC
{
	vector<int*> myV;
public:
	VecC( int xx)
	{
		int *wtf = new int(xx);
		myV.push_back(wtf);
		
	};

	const vector<int*> & getV() { return myV; };
};

int main( int argc, char* argv[] )
{
	VecC obj(5);

	const vector<int*> & ref = obj.getV();

	*ref.at(0) = 9; // I WANT THIS TO NOT BE ALLOWED
}

Is this possible?
Thx in advance

If you want the ints to be read only, make them const also (requires the following casting):

const vector<const int*> &  getV() { return *(const vector<const int*>*)&myV; };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.