This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
I saw following code on c++ reference:
bool operator() (const int& lhs, const int&rhs) const
What does the last const do?
From the Const Correctness tutorial:
If you have a const object, you don't want to call methods that can
change the object, so you need a way of letting the compiler know
which methods can be safely called. These methods are called "const
functions", and are the only functions that can be called on a const
object. Note, by the way, that only member methods make sense as const
methods. Remember that in C++, every method of an object receives an
implicit this pointer to the object; const methods effectively receive
a const this pointer.
It might be worthwhile (spoiler: it is) to read through the whole article if you're new to the concept of constness.
It means the function (operator) does not change the object.
Effectively makes the "this" pointer a pointer to a const object. It means that members of the object cannot be modified in that method, nor can that method be invoked on a non-const object.
Related
This question already has answers here:
Same function with const and without - When and why?
(7 answers)
Closed 6 years ago.
I was making a class for arrays to work as stacks and encountered upon two types of functions to return the top element. I cannot understand the difference between the two and how the compiler decides which one of the two to call. Beloww is the code for the two.
T & getTop() { //function 1
return arr[top - 1];
}
const T & getTop() const { //function 2
return arr[top - 1];
the 'top' variable points to the current empty cell in the array and T is the generic datatype.
Thank you for your help in advance.
I suppose your stack is called stack.
stack<T> s;
/*do something with it*/
s.getTop(); //will call the non const version
std::as_const(s).getTop() //will call const version
And similarly:
const stack<T> s;
s.getTop() //const version
So, if the type of the variable is not const, it will call non const version. Otherwise it will invoke const version.
You can see this answer for reference but in a few words for this specific case looks like is for tell to others methods won't change the logical state of the object.
A const T& reference doesn't allow to modify the referenced data. Notice that the second getTop() method is declared as const too to specify that the method doesn't modify the state of the instance on which this is called.
Two implementations are required to allow getting the top element from a context in which the instance that contains the array is const and at the same time allowing to modify the top element in contexts in which it's not const.
This is part of a bigger concept named const correctness, you can get additional info about it here, specifically:
What does “const X& x” mean?
What is a “const member function”?
What is the relationship between a return-by-reference and a const member function?
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 6 years ago.
When coding a header file in C++ with method declarations, what's the difference between:
int getFoo() const;
const int getFoo();
const int getFoo() const;
First one, is for preventing this method changing any member variables of the object.
Second one, is for the return type (ie: constant integer)
Third one, is mix of both
Your first function operates on a const this pointer (that is; a const object that it can't change (or at least shouldn't)).
Your second function returns a constant integer - which is somewhat nonsensical since you can just assign it to a non-const variable and change it anyway. Besides, why does the function care if you change a POD type or not?
Your third function is just a combination of the first two. A function operating on a const object returning a const value.
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
I saw something like:
const char& operator[] (int Index) const
the first const I understand. It's to protect the returning char from being modified.
But what does second const mean? Why do we sometimes use two const, sometimes just one?
It can be used for any member function, not only operators. It means, that this function will:
not be able to modify any data members (except mutable ones)
will not be able to call any non-const member functions
This question already has answers here:
what does const mean in c++ in different places
(4 answers)
Functions returning a const value [duplicate]
(1 answer)
Closed 9 years ago.
This code looks weird:
const double square(double x) {
return x*x;
}
Common sense tells me that const in this context means either
the function returns a const double
OR
it promises not to change the double that was passed in. But it was passed in by value! I don't understand
OR
I know that const member functions promise not to change *this, but this is not even a member function.
Edit
What's the point to return a const double if you can save the result in a non-const variable and edit it??
double var = square(4.5); // no compile error
var = 0.3;
It is weird. As you say:
the function returns a const double
For a user-defined type, this would prevent you from modifying the return value:
square(2) += 5; // Error
although, as noted in the comments, this isn't allowed for a built-in type like double anyway. Even for user-defined types, while you probably don't want to write code like that, there's no point preventing it either.
For more complicated types, returning a const value can actually be harmful: it inhibits move semantics, since a const object can't be moved from. This could hurt performance.
That is not a "const function". It is a function which returns a value that is const. A "const function" only applies to member functions, and then the const goes on the right, after the parameter list.
The utility of this for a simple value type is dubious. However, if you were returning a reference to const or a const pointer, or something else, then it could have merit.
The only thing that returning a const of a simple type by value does is make sure that it can't be captured by non-const r-value reference in C++11. Otherwise, they'll just copy the value and be on their way.
In the example you give, and for any POD type, it makes little sense, but for more complex types it can make sense:-
const SomeClass &SomeFunction ();
which returns a reference to a const object.
A const function with const before the return value is usually used where the function returns a pointer (or perhaps a reference) to say that it will only return const pointers. This one returns a double and that means it only returns doubles that are const.
It returns a const double type variable.
If you do it like this, most compilers give a warning if you try to assign the returned value to a double type.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
const CFoo &bar() const
Which is the meaning of this line?
virtual void Encode (KDataStream & stream) const;
What´s the meaning of const at the end in C++?
Which is the meaning of this line?
virtual void Encode (KDataStream & stream) const;
It's a statement that declares a function.
virtual means it's a member function that can be overridden by a function of the same name and compatible parameter and return types declared in a class derived from this one. The correct version will be chosen (at run-time, if necessary) according to the type of the object it's invoked on.
void means it doesn't return anything.
Encode is the name of the function.
( marks the start of the parameter list.
KDataStream is the type of the first parameter.
& means the parameter is passed by reference.
stream is the name given to the parameter; it serves as documentation, but can be left out of the declaration without changing the meaning.
) marks the end of the parameter list.
const means that it's a member function that can't modify non-static, non-mutable data members of the object it's invoked on. It also allows it to be invoked on objects that are declared const.
; marks the end of the statement.
It means -- pass by reference.
The 'const' at the end of the method says that the method implementation will not change the values of any member variables. So, by seeing this in the class interface itself (without having to know the implementation), the clients of the object can know about this behaviour.
Read up on pointers, if you want to code in c++ you will need to know how these work:
http://www.cplusplus.com/doc/tutorial/pointers/
& means you are passing in the memory address of stream rather the value of stream