const correctness code

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • const correctness code

      Hi All and Mr Mike
      I already bought code complete book and so far it give me enlightment of game programming subject.
      Right now I still learn to code const correctness and I found it code in this book like this:

      char const * const gStringSomething;
      int const * const gIntSomething;

      what is code above means?
      As far as i know code const is like this:
      const int gIntSomething; or int const gIntSomething;

      what the different between one single const with double const like that.
      I already check in MSDN and i didn't found good answer.

      Please help me find the way to understand that.

      Thanks before.
    • RE: const correctness code

      Well, with a pointer, you really have two different data:

      1) The address of the data
      2) The data that the pointer points to.

      If you say const, which is really constant? Does it mean you can't change the pointer itself to point to a different data set, or does it mean that you can change the data that the pointer is pointing to? In C++, you can set it either way.

      Double const means you can't change either.
      -Larrik Jaerico

      www.LarrikJ.com
    • RE: const correctness code

      I'd also like to add that const doesn't just provide a safety net. The optimizer can do some magic things with const values since it knows the value will never change. One example is declaring something like this at the top of a source file:

      Source Code

      1. const int NUM_KITTENS = 20;


      The optimizer will most likely treat this as a #define and simply replace all the instances of NUM_KITTENS in your code with the number 20, thus saving you four bytes of memory and the overhead of reading from memory as opposed to simply having the number in code. ot sure if it does this every time, but I think it does more often than not. There are several other optimizations const will give you, but I can't remember any more. None of them are huge, but every little bit helps. :)

      Time to go home and sleep now....

      -Rez
    • RE: const correctness code

      Dood - Rez - 4am????

      Man oh man....
      That Barbie is a HARSH mistress...
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • RE: const correctness code

      I'm a trooper, what can I say? ;) Alpha in less then a week. Alpha means money, plus it's when QA starts so this is a huge milestone. Welcome to crunch time, gentlemen!

      -Rez