What is the benefit of using const for declaring constants?

The benefit of using the const keyword is that the compiler might be able to make optimizations based on the knowledge that the value of the variable will not change. In addition, the compiler will try to ensure that the values won’t be changed inadvertently. Of course, the same benefits apply to #defined constants. The reason to use const rather than #define to define a constant is that a const variable can be of any type (such as a struct, which can’t be represented by a #defined constant). Also, because a const variable is a real variable, it has an address that can be used, if needed, and it resides in only one place in memory  

Showing Answers 1 - 10 of 10 Answers

paulson paul chambakottukudyil

  • Apr 21st, 2006
 

If we use const variable instead of #define, we can specify the scope of the variable.

hurdjg

  • May 30th, 2007
 

A side effect benefit:
Say you want to pass an argument that you do not want modified, but you do not want to pass by value.
Solution pass as constant reference or pointer constant.

int someFoo(myDataType const &chunkOfData);
or
int someFoo(myDataType const * pchunkOfData);

So chunkOfData is safe from being modified and the call is much faster than a pass by value.


Note the * is on the right side of const so the pointer is not constant but what it is currently pointing to is.
If the * is on the left of const then it is constant pointer, a label associated to an address like the function someFoo(args) is a constant pointer or any other function.

  Was this answer useful?  Yes

hurdjg

  • May 30th, 2007
 

A side effect benefit:
Say you want to pass an argument that you do not want modified, but you do not want to pass by value.
Solution pass as constant reference or pointer constant.

int someFoo(myDataType const &chunkOfData);
or
int someFoo(myDataType const * pchunkOfData);

So chunkOfData is safe from being modified and the call is much faster than a pass by value.


Note the * is on the right side of const so the pointer is not constant but what it is currently pointing to is.
If the * is on the left of const then it is constant pointer, a label associated to an address like the function someFoo(args) is a constant pointer or any other function.

  Was this answer useful?  Yes

hurdjg

  • May 30th, 2007
 

A side effect benefit:
Say you want to pass an argument that you do not want modified, but you do not want to pass by value.
Solution pass as constant reference or pointer constant.

int someFoo(myDataType const &chunkOfData);
or
int someFoo(myDataType const * pchunkOfData);

So chunkOfData is safe from being modified and the call is much faster than a pass by value.


Note the * is on the right side of const so the pointer is not constant but what it is currently pointing to is.
If the * is on the left of const then it is constant pointer, a label associated to an address like the function someFoo(args) is a constant pointer or any other function.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions