Skip to content

Latest commit

 

History

History
197 lines (153 loc) · 6.4 KB

const-cpp.md

File metadata and controls

197 lines (153 loc) · 6.4 KB
descriptiontitlems.datef1_keywordshelpviewer_keywordsms.assetid
Learn more about: const (C++)
const (C++)
09/27/2022
const_cpp
const keyword [C++]
b21c0271-1ad0-40a0-b21c-5e812bba0318

const (C++)

When it modifies a data declaration, the const keyword specifies that the object or variable isn't modifiable.

Syntax

declarator:
ptr-declarator
noptr-declaratorparameters-and-qualifierstrailing-return-type
ptr-declarator:
noptr-declarator
ptr-operatorptr-declarator
noptr-declarator:
declarator-idattribute-specifier-seqopt
noptr-declaratorparameters-and-qualifiers
noptr-declarator[constant-expressionopt]attribute-specifier-seqopt
(ptr-declarator)
parameters-and-qualifiers:
(parameter-declaration-clause)cv-qualifier-seqopt
ref-qualifieroptnoexcept-specifieroptattribute-specifier-seqopt
trailing-return-type:
->type-id
ptr-operator:
*attribute-specifier-seqoptcv-qualifier-seqopt
&attribute-specifier-seqopt
&&attribute-specifier-seqopt
nested-name-specifier*attribute-specifier-seqoptcv-qualifier-seqopt
cv-qualifier-seq:
cv-qualifiercv-qualifier-seqopt
cv-qualifier:
const
volatile
ref-qualifier:
&
&&
declarator-id:
...optid-expression

const values

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

// constant_values1.cppintmain() { constint i = 5; i = 10; // C3892 i++; // C2105 }

In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined with const are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with a const variable as follows:

// constant_values2.cpp// compile with: /cconstint maxarray = 255; char store_char[maxarray]; // allowed in C++; not allowed in C

In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.

The const keyword can also be used in pointer declarations.

// constant_values3.cppintmain() { char this_char{'a'}, that_char{'b'}; char *mybuf = &this_char, *yourbuf = &that_char; char *const aptr = mybuf; *aptr = 'c'; // OK aptr = yourbuf; // C3892 }

A pointer to a variable declared as const can be assigned only to a pointer that is also declared as const.

// constant_values4.cpp #include<stdio.h>intmain() { constchar *mybuf = "test"; char *yourbuf = "test2"; printf_s("%s\n", mybuf); constchar *bptr = mybuf; // Pointer to constant dataprintf_s("%s\n", bptr); // *bptr = 'a'; // Error }

You can use pointers to constant data as function parameters to prevent the function from modifying a parameter passed through a pointer.

For objects that are declared as const, you can only call constant member functions. The compiler ensures that the constant object is never modified.

birthday.getMonth(); // Okay birthday.setMonth( 4 ); // Error

You can call either constant or non-constant member functions for a non-constant object. You can also overload a member function using the const keyword; this feature allows a different version of the function to be called for constant and non-constant objects.

You can't declare constructors or destructors with the const keyword.

const member functions

Declaring a member function with the const keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant. To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition.

// constant_member_function.cppclassDate { public:Date( int mn, int dy, int yr ); intgetMonth() const; // A read-only functionvoidsetMonth( int mn ); // A write function; can't be constprivate:int month; }; intDate::getMonth() const { return month; // Doesn't modify anything } voidDate::setMonth( int mn ) { month = mn; // Modifies data member } intmain() { Date MyDate( 7, 4, 1998 ); const Date BirthDate( 1, 18, 1953 ); MyDate.setMonth( 4 ); // Okay BirthDate.getMonth(); // Okay BirthDate.setMonth( 4 ); // C2662 Error }

C and C++ const differences

When you define a const variable in a C source code file, you do so as:

constinti=2;

You can then use this variable in another module as follows:

externconstinti;

But to get the same behavior in C++, you must define your const variable as:

externconstint i = 2;

Similar to C, you can then use this variable in another module as follows:

externconstint i;

If you wish to define an extern variable in a C++ source code file for use in a C source code file, use:

extern"C"constint x=10;

to prevent name mangling by the C++ compiler.

Remarks

When following a member function's parameter list, the const keyword specifies that the function doesn't modify the object for which it's invoked.

For more information on const, see the following articles:

See also

Keywords

close