Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using the const keyword before and after method or function name?

Tags:

c++

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_; } 
like image 536
Waleed Mohsin Avatar asked May 08 '13 20:05

Waleed Mohsin


People also ask

Can const be used before a function name?

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.

Why do we use const in functions?

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.

What is the meaning of const after function name?

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.

What is const before a 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.

What are the functions of the const keyword in C++?

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.

What happens when a function is declared as const in C++?

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.

How to declare a pointer with a const keyword in C++?

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:

Can we talk about const without mentioning the final keyword?

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.


1 Answers

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' } 
like image 92
stardust Avatar answered Sep 22 '22 20:09

stardust