Summary: In this tutorial, we will learn what the Const objects and functions are in C++, what their significance is, and when we should use them in our programs.
What is Const Object?
C++ is an object-oriented programming language and we often write code using classes and objects but sometimes we do not intend to change the value of the object’s property in any circumstances.
In that case, we declare the object as constant using the const
keyword in our C++ program.
By doing this, the properties of the object once initialized cannot be changed further.
If an attempt is made to change the value of any attribute of the const
object, the compiler will throw the error.
Consider the following example for instance:
#include <iostream>
using namespace std;
class Dog{
public:
string name;
string breed;
Dog(string name, string breed){
this->name = name;
this->breed = breed;
}
};
int main() {
//create an object
Dog dog("Scooby", "Breed-1");
cout << dog.name << " is of " << dog.breed << endl;
//change it's properties
dog.breed = "Breed-2";
cout << dog.name << " is of " << dog.breed << endl;
}
Output:
Scooby is of Breed-1Scooby is of Breed-2
In the above example, we have explicitly changed the value of the breed
of the dog
object.
We don’t want the object to be modified after it has been initialized with values, not even by mistake.
To solve the above ambiguity, we can either mark every property as private
or declare them constant inside the class. Doing so would impose the same restriction on all instances of the Dog
class.
So to restrict only certain objects from modification we use const
objects because the const
object attributes cannot be changed once initialized with values.
Here is the code snippet where we have created an constant object in C++:
int main() {
const Dog dog("Scooby", "Breed-1");
cout << dog.name << " is of " << dog.breed << endl;
dog.breed = "Breed-2"; //Error
cout << dog.name << " is of " << dog.breed << endl;
}
If we now try to change the value of breed
, the compiler will throw an error.
What is Const Function?
A const function is created when the declaration of any function is postfixed with the const
keyword.
For example:
void function_name const {
// code
}
It is used mostly for const
objects because if we try to call the member function (which is non-const) through a const
object, the compiler will throw an error.
This is because the compiler assumes that the member function can potentially modify the current object data, even if the member function does not intend to do so.
For example, consider the following program:
class Dog{
string name;
string breed;
public:
Dog(string name, string breed){
this->name = name;
this->breed = breed;
}
//For returning the data
string getName(){const Dog &dog}{
return dog.name;
}
};
int main() {
const Dog dog("Scooby", "Breed-1");
cout << dog.getName() << endl; //Error
}
If we compile this code, we will get the following error as output:
error: ‘this’ argument to memberfunction ‘getName’ has type ‘const Dog’, but function is not marked const
To solve this issue, we modify the function to a const
function.
class Dog{
string name;
string breed;
public:
Dog(string name, string breed){
this->name = name;
this->breed = breed;
}
string getName() const{
return this->name;
}
};
int main() {
const Dog dog("Scooby", "Breed-1");
cout << dog.getName() << endl;
}
Output: Scobby
The const
function in C++ assures the compiler that the function will only read the object passed as an argument, it will not modify it.
As a programmer, It is important to not modify any property of the object data in the const function otherwise the compiler will raise an error.
In this tutorial, we learned about the const object and const function in the C++ programming language.
hi
could you please explain below code of line
string getName(){const Dog &dog}