In C++, functions can be categorized as const or non-const depending on whether they are allowed to modify the object on which they are called. The concept of const in C++ is crucial for ensuring that certain member functions or methods do not change the state of an object, thus providing safety and clarity in the code.
A non-const member function is a regular member function that can modify the state of the object it belongs to. This means it can alter the values of the member variables of the class.
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
Car(string b, int y) : brand(b), year(y) {}
// Non-const member function that modifies the object
void setBrand(string b) {
brand = b; // Modifies the object's state
}
void displayDetails() const {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1("Toyota", 2020);
car1.setBrand("Honda"); // Calling non-const function
car1.displayDetails();
return 0;
}
setBrand() function is a non-const member function that modifies the brand of the Car object.car1), allowing the object’s state to be modified.A const member function is a member function that guarantees not to modify the state of the object. This means that within a const member function, you cannot alter the values of the member variables. It is useful when you want to ensure that the function does not change the object, maintaining the integrity of the object’s state.
returnType functionName() const;
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
Car(string b, int y) : brand(b), year(y) {}
// Const member function that cannot modify the object
void displayDetails() const {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
// Const function that can access only const members
string getBrand() const {
return brand; // Can read but not modify brand
}
// Non-const function that modifies the object
void setYear(int y) {
year = y; // Modifies the year
}
};
int main() {
const Car car1("Toyota", 2020);
car1.displayDetails(); // Calling const function
// car1.setYear(2021); // Error: Cannot call non-const function on a const object
cout << "Brand: " << car1.getBrand() << endl;
return 0;
}
displayDetails() and getBrand() are const member functions, meaning they do not modify the object’s state.car1 is a const object, so it can only call const functions.setYear() on a const object results in a compilation error because setYear() is a non-const function that modifies the object.Access to Non-Const Members:
Called on Const Objects:
const object, because it could potentially modify the object.const and non-const objects, because it guarantees not to modify the object.Const-Correctness:
Compiler Enforcement:
const.Const Functions: These are useful when you want to ensure that a function does not modify the state of the object. For example:
Non-Const Functions: These are needed when the function needs to modify the object's state. For example:
| Aspect | Const Function | Non-Const Function |
|---|---|---|
| Modification of Object | Cannot modify the object | Can modify the object |
| Allowed on const objects | Yes, can be called on const objects | No, cannot be called on const objects |
| Access to Members | Can only access const members | Can access both const and non-const members |
| Usage | To guarantee the object’s state is not modified | When the object’s state needs to be changed |
| Syntax | returnType func() const; |
returnType func(); |
In C++, const and non-const member functions provide mechanisms for controlling whether an object can be modified. Const member functions are important for ensuring that certain operations are safe and do not alter the object, while non-const functions are used when modification of the object’s state is necessary. By using const functions appropriately, we ensure const-correctness, which leads to more robust and reliable code.
Open this section to load past papers