Jump to content
  • Advertisement
Sign in to follow this  
taz0010

Const correctness problem

This topic is 3054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!