![]() |
All about const in C++
IntroductionThis article talks about all different usage of const qualifier in C++. BackgroundDeclaration: Named constant or const variables const int PI = 3.14 Declares a named constant called PI, which can be used later in the program where ever we want to use the value 3.14. This is similar to #define macro, but using const is better as it is understood and used by the compiler itself and not just simple substitution in the program which is done by preprocessor on using #define. const with pointers: Here one has to be careful in using const to know whether it is used to determine the
To declare a pointer that is pointing to a constant interger const int *ptrToConstData1; int const *ptrToConstData2; Here, we cannot change the data it is pointing to. To declare a const pointer that is pointing to an integer int * const ptrIsConst; This has to be declared and initialised and cannot be changed later to point to some other variable. To declare a const pointer pointing to constant integer const int * const ptrAndDataCnst; Here both pointer and data are constant. As it is also a constant pointer, it has to be initialised while declaring itself. The codeCode: Cpp
const Data Member: Any const data members of a class has to be initialized and has to be done in the initilizer list of the class's constructor. Only static const data members can be intialized inside the class declaration. The codeCode: Cpp
Return by const value: Return by const is meaningful in case of returning:- - const strings and arrays from functions as they are implemented as pointers. This is to avoid the program altering their lValue and getting crash. - If a function is returning any user-defined data types like class objects. Where as It does not make sense returning const to a built-in types, as compiler anyway takes care of it. In above cases by making that function returning a const will prevent using that returned value as an lValue in program as it will be a compier error. Otherwise, it will allow using that returned value as lValue. The codeCode: Cpp
const variable as an argument: To make sure, preventing the data that is passed as argument, whether it could be a reference or pointer variable (which was sent to avoid sending by value as copying in case of big user defined data is costly), getting aletred inside the function or callee, then we can send that data by const qualifier. The codeCode: Cpp
As shown in the example, to ease the burden of caller not sending data as const, we can even implement inside the function by making a const reference to the sent value and then prevent alering it. const Member Functions: Declaring as below makes that method as const int myInt() const it means, function is not allowed to modify any of the data members of that class object. Normally, get accessors are written as const methods. The codeCode: Cpp
const int*const myFunction(const int*const&)const This one is an example of a const method, that will return a constant pointer, pointing to a const integer, also it does not alter any of passed arguments to it. Related terms to const operator const_cast < > This is used to remove the constant quality of a variable or object. The codeCode: Cpp
As shown in the above example, case-1 we can cast away the constness of a variable or object, that is not explicitly declared with const qualifier. Where as in case-2, if we try to cast away constness of a variable or object that has been explictly declared as const, results in undefined bahaviour. Also, below is another example showing the usage of const_cast in a class's const member function. The codeCode: Cpp
Here, in this example, const_cast allows to modify the member of a class in a constant member function. mutable keyword Declaring a member variable as mutable allows that particular variable to be modified in a const member function. It means, it allows the declared member of a constant object to be modified. This concept is used, when most of the data members of a class are kept const, but few need to be updatable, then declare those updatable members as mutable. The codeCode: Cpp
ReferencesAlso Refer: Constant and pointer http://www.go4expert.com/showthread.php?t=1578 |
Re: All about const in C++
Very nice article, As expected from you....
|
Re: All about const in C++
Many thanks mayjune !!
Then please vote this artcile, if it gets nominated in the August month's competetion!! |
Re: All about const in C++
But you posted it in September? It'll get selected in September na?
|
Re: All about const in C++
Thanks for the very nice article and very very thanks for the pasting a code in it.
|
Re: All about const in C++
@mayjune
For september's competetion :-) many thanks michaelsm |
Re: All about const in C++
Cool
|
Re: All about const in C++
Guys i need your help T_T my professor gave me a problem and i don't know how to do it, pls help me
"Creat a program that will compute for the grade of students and its equivalent grade point.. the formula for computing grade is GRADE=10%, ASSIGNMENT=20%,Seatwork=30%,Quiz=40% Note that there are 2 assignment,quiz,and seatwork given Equivalent grade point: 1.0 = 99-100 1.25 = 96-98 1.5 = 93-95 1.75 = 90-92 2.0 = 87-89 2.25 = 84-86 2.50 = 81-83 2.75 = 78-80 3.0 = 75-77 5.0 = below 75 thanks, i hope you can help me T_T God Bless! |
Re: All about const in C++
matrix_jc2001, Do not jump into any thread with that and I see you already have a thread for it.
|
Re: All about const in C++
Nomination for Article of the month - Sep 2009 Started. Nominate this article.
|
| All times are GMT +5.5. The time now is 19:41. |