Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of const in C++? [duplicate]

Tags:

c++

constants

What are the advantages of const in C++ (and C) for the uninitiated?

like image 339
casualcoder Avatar asked Jan 22 '09 02:01

casualcoder


People also ask

What is the advantage of using const?

Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.

What are the advantages of using the const keyword rather than #define?

The big advantage of const over #define is type checking. #defines can't be type checked, so this can cause problems when trying to determine the data type. If the variable is, instead, a constant then we can grab the type of the data that is stored in that constant variable.

Which are advantages of using a constant property const?

Constants can make your program more readable. For example, you can declare: Const PI = 3.141592654. Then, within the body of your program, you can make calculations that have something to do with a circle. Constants can make your program more readable.

What is the purpose of const in C?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it. In C, constant values default to external linkage, so they can appear only in source files.

What is the advantage of const over define in C++?

The big advantage of const over #define is type checking. #defines can’t be type checked, so this can cause problems when trying to determine the data type. If the variable is, instead, a constant then we can grab the type of the data that is stored in that constant variable. Since const are considered variables, we can use pointers on them.

What is the use of const variable in C?

C const In C const is the keyword to create constants (variables which don’t change their value). Normally the usage of const is straightforward, but it becomes tricky when used with pointers. 30% Off my course, 30 days money back guarantee!

What are the advantages and disadvantages of using const variable?

The big advantage of const over #define is type checking. We can also have poitners to const varaibles, we can pass them around, typecast them and any other thing that can be done with a normal variable. One disadvantage that one could think of is extra space for variable which is immaterial due to optimizations done by compilers.

What is the use of const keyword in C++?

Whenever const keyword is attached with any method (), variable, pointer variable, and with the object of a class it prevents that specific object/method ()/variable to modify its data items value. There are a certain set of rules for the declaration and initialization of the constant variables:


2 Answers

Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.

See also: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4

like image 151
DWright Avatar answered Oct 12 '22 19:10

DWright


When used as a const reference in a function, it lets the caller know that the thing being passed in won't be modified.

void f(Foo &foo, const Bar &bar) { ... }

In this case the caller will know that the foo might be modified, but the bar will not. The compiler will enforce this when compiling the body of f(), so that bar is never modified and never passed on to another function that might modify it.

All of the above safeguards can be bypassed using const_cast, which is why such a cast is considered "dangerous" (or at the very least, suspicious).

like image 35
Greg Hewgill Avatar answered Oct 12 '22 20:10

Greg Hewgill