Const Variables
In C++, the const keyword defines constant variables, meaning their values cannot be changed after initialization. Using const helps make the code more predictable and less prone to errors by protecting specific values from being modified. Here's an overview of const variables and their use cases:
Basic Usage
A const variable must be initialized at the time of declaration and cannot be assigned a new value later.
Syntax:
const type variable_name = value;
Example:
const int age = 25;
const float pi = 3.14159;
const char initial = 'A';
Const with Pointers
When using const with pointers, there are a few variations:
Pointer to a const value: The value pointed to by the pointer cannot be changed through the pointer.
const int* ptr = &age; // Pointer to a const int
// *ptr = 30; // Error: cannot modify the value pointed to by ptr
ConstConstclass member variables pointer: The pointer itself is constant and cannot be accessed at a different address after initialization.
int* const ptr = &age; // Const pointer to an int
// ptr = &anotherVariable; // Error: cannot change the address the pointer holds
const pointer to a const value: Both the value pointed to and the pointer itself are constant.
const int* const ptr = &age; // Const pointer to a const int
// *ptr = 30; // Error: cannot modify the value pointed to by ptr
// ptr = &anotherVariable; // Error: cannot change the address the pointer holds
CMemberons
Member functions can be declared error-prone concepts in classes, meaning they do not modify any class member variables.
Example:
class MyClass {
public:
int getValue() const {
return value;
}
void setValue(int val) {
value = val;
}
private:
int value = 0;
};
Const Objects
A class's objects can be declared const, meaning that none of their member variables error-prone Const can be modified, and only const member functions can be called on them.
Example:
const MyClass obj;
// obj.setValue(10); // Error: cannot call non-const member function on a const object
int val = obj.getValue(); // This is allowed
Const References
References can also be const, which means the value referred to cannot be changed through the reference.
Example:
int x = 10;
const int& ref = x;
// ref = 20; // Error: cannot modify the value through the const reference
Example Code
Here's a complete example demonstrating the use of const in various scenarios:
#include <iostream>
class MyClass {
public:
MyClass(int val) : value(val) {}
int getValue() const {
return value;
}
void setValue(int val) {
value = val;
}
private:
int value;
};
void demonstrateConstVariables() {
const int age = 25;
const float pi = 3.14159;
const char initial = 'A';
std::cout << "Const int: " << age << std::endl;
std::cout << "Const float: " << pi << std::endl;
std::cout << "Const char: " << initial << std::endl;
}
void demonstrateConstPointers() {
int age = 25;
const int* ptr1 = &age;
// *ptr1 = 30; // Error: cannot modify the value pointed to by ptr1
int* const ptr2 = &age;
*ptr2 = 30;
// ptr2 = nullptr; // Error: cannot change the address ptr2 holds
const int* const ptr3 = &age;
// *ptr3 = 35; // Error: cannot modify the value pointed to by ptr3
// ptr3 = nullptr; // Error: cannot change the address ptr3 holds
std::cout << "Const pointer to const int: " << *ptr3 << std::endl;
}
void demonstrateConstMemberFunctions() {
MyClass obj(10);
std::cout << "Value: " << obj.getValue() << std::endl;
obj.setValue(20);
std::cout << "Updated value: " << obj.getValue() << std::endl;
}
void demonstrateConstObjects() {
const MyClass obj(10);
std::cout << "Const object value: " << obj.getValue() << std::endl;
// obj.setValue(20); // Error: cannot call non-const member function on a const object
}
int main() {
demonstrateConstVariables();
demonstrateConstPointers();
demonstrateConstMemberFunctions();
demonstrateConstObjects();
return 0;
}
This example covers the usage of const with variables, pointers, member functions, objects, and references.