Sign in to follow this  
taz0010

Const correctness problem

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Found it! I was putting the const on the wrong side of the expression.


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

Problem solved.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this