Original Post
Quite simply, why should I use it ever? What's it's reason for existance?
Quote:Thanks for the endorsement! [smile]
Original post by Boku San
There's probably more, and I bet Oluseyi can point them out for you. He'ssmertsmart.
const int * p = &someIntVariableconst int const * p2 = &someIntVariableThe first allows the modification of the value pointed to, but not the pointer itself. The second allows neither.
Quote:Has anyone else heard of consts taking up less memory??
Original post by Roboguy
Also it takes up less memory.
Quote:
From Herb Sutters article:
It's a common belief that const-correctness helps compilers generate tighter code. Const is indeed a Good Thing, but the point of this issue of GotW is that const is mainly for humans, rather than for compilers and optimizers. When it comes to writing safe code, const is a great tool that lets programmers write safer code with compiler checking and enforcement. Even when it comes to optimization, const is still principally useful as a tool that lets human class designers better implement handcrafted optimizations, and less so as a tag for omniscient compilers to automatically generate better code.
Quote:
Original post by Oluseyi
One additional caveat to using const effectively is pointers. Consider the following:The first allows the modification of the value pointed to, but not the pointer itself. The second allows neither.const int * p = &someIntVariableconst int const * p2 = &someIntVariable
const int * p = &someIntVariableconst int * const p2 = &someIntVariableclass A{public: A() : x(0) {} int x;};int main(int argc,char* argv[]){ A array[2]; A* p = array; p->x++; // <- Allowed p++; // <- also allowed const A* cp1 = array; A const* cp2 = array; cp1->x++; // <- Disallowed, Object is const cp1++; // <- Allowed, pointer is mutable cp2->x++; // <- Disallowed, Object is const cp2++; // <- Allowed, pointer is mutable A* const pc1 = array; const A* const pc2 = array; pc1->x++; // <- Allowed, Object is mutable pc1++; // <- Disallowed, pointer is const pc2->x++; // <- Disallowed, Object is const pc2++; // <- Disallowed, pointer is const return 0;}Quote:
Original post by DudeMiester
Also, if I have a function:
const T f(){return T();}
Will the compiler skip over T's copy constructor and basically turn const T f() into T& const f(), but without the issue of returning a temporary?
Quote:
Original post by Oluseyi
const int PI = 3.142;
Quote:
Original post by UfoZ Quote:
Original post by Oluseyi
const int PI = 3.142;
Is that an Alabama pi?
[grin]
Quote:
Original post by silvermace
_ROFL_, i love typos!
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more