Const correctness problem

Started by
4 comments, last by alvaro 14 years, 1 month ago

struct Foo{
	int bar;
};

int main(){
	const Foo f;
	const Foo* pF = &f
	Foo** fpArray = new Foo*[10];
	fpArray[4] = &f
}
The last line is complaining because fpArray is an array of mutable foo objects and I try to store a const one. But applying const to the definition isn't fixing the problem. How do you create an array of pointers to a const object, and add to this array?
Advertisement
There are several ways to "apply const to the definition" and you are probsbly using the wrong one. What is it exactly that you tried?
I've tried everything. Can you write down the code that works?
Using the STL works by the way. So the following compiles without problems.

const Foo f;std::vector<const Foo*> vpF(10);vpF[1] = &f


I could just use the STL of course but I'd rather know how to do this manually.
Maybe this will help?
Found it! I was putting the const on the wrong side of the expression.


const Foo** fpArray = new const Foo*[10];

Problem solved.
Quote:Original post by taz0010
Found it! I was putting the const on the wrong side of the expression.


const Foo** fpArray = new const Foo*[10];

Problem solved.


I'm glad you solved it. Notice that you never posted what you were doing wrong, only a vague description.

This topic is closed to new replies.

Advertisement