I have the following code in my application. Why do we use the const
keyword with the return type and after the method name?
const T& data() const { return data_; }
A const object can be created by prefixing the const keyword to the object declaration. Any attempt to change the data member of const objects results in a compile-time error. When a function is declared as const, it can be called on any type of object, const object as well as non-const objects.
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.
Making a member function const means that. it cannot call any non-const member functions. it cannot change any member variables. it can be called by a const object( const objects can only call const functions). Non-const objects can also call a const function.
const T& data() const { return data_; } const after member function indicates that data is a constant member function and in this member function no data members are modified. const return type indicates returning a constant ref to T. Follow this answer to receive notifications.
In this article, the various functions of the const keyword which is found in C++ are discussed. Whenever const keyword is attached with any method (), variable, pointer variable, and with the object of a class it prevents that specific object/method ()/variable to modify its data items value.
An object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object. When a function is declared as const, it can be called on any type of object, const object as well as non-const objects.
Const Keyword With Pointer Variables: Pointers can be declared with a const keyword. So, there are three possible ways to use a const keyword with a pointer, which are as follows: When the pointer variable point to a const value: Syntax: const data_type* var_name; Below is the C++ program to implement the above concept:
I would have concluded my article here, but there is no talking of const without mentioning the final keyword since many developers tend to use them interchangeably and most often when face with the choice of choosing always resort to final rather than const.
const T& get_data() const { return data_; } ^^^^^
means it will return a const
reference to T
(here data_
)
Class c; T& t = c.get_data() // Not allowed. const T& tc = c.get_data() // OK.
const T& get_data() const { return data_; } ^^^^^
means the method will not modify any member variables of the class (unless the member is mutable
).
void Class::get_data() const { this->data_ = ...; // is not allowed here since get_data() is const (unless 'data_' is mutable) this->anything = ... // Not allowed unless the thing is 'mutable' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With