Jump to content
  • Advertisement
Sign in to follow this  
ms75214

const trivia

This topic is 2498 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

say you have a c++ program with this:

const float pi = 3.14;

then you have another program, exactly the same, but without the "const" there.

will they compile to exactly the same thing?

Share this post


Link to post
Share on other sites
Advertisement
The result will be the same. The only thing that differs is you can't change the variable that's been marked const after declaring it. Try to change it after declaration and then compile. ;)

Share this post


Link to post
Share on other sites

say you have a c++ program with this:

const float pi = 3.14;

then you have another program, exactly the same, but without the "const" there.

will they compile to exactly the same thing?

Yes.
No.
Maybe.
Perhaps.
Possibly.
Certainly.
Never.

All of the above are correct answers for certain conditions and compilers. This is entirely implementation defined.

Share this post


Link to post
Share on other sites
It also depends on what scope it's declared in. If it's a global variable then a const variable has implicit internal linkage while a non-const variable has implicit external linkage. This difference will do a lot to drive what code the compiler will generate to use the variable.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!