3

The meaning of & ldquo; const & rdquo; in a declaration of function

 2 years ago
source link: https://www.codesd.com/item/the-meaning-of-const-in-a-declaration-of-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The meaning of & ldquo; const & rdquo; in a declaration of function

advertisements

This question already has an answer here:

  • Const correctness: const char const * const GetName const (//stuff); 8 answers

I have a function with 3 const's in it:

const std::string at(const unsigned int index) const;

To my understanding, the parameter (const unsigned int index ) means that index will not be changed. What about the other two const's? Why are they there?


The const keywoard in C++ indicates that a particular object or variable is not modifiable. It can be used in various contexts:

Const variables

Declaring a variable as const inside of a function indicates that the variable will not be modified inside the function.

Const member functions

Declaring a member function as const, which is done by appending const to the end of the function prototype, indicates that the function is a "read-only" function that does not modify the object for which it is called.


The rule for const viability is that const-ness can be applied to a non-const variable or member function, but once applied it cannot be removed.

Tags const

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK