This question already has answers here:
Why using the const keyword before and after method or function name?
(4 answers)
Closed 9 years ago.
I am working on a class project and my teacher has given us functions that we must write. Here is an example of one. I am wondering why there is a const at the beginning and the end. What do they do that they are needed on either end?
const Foo multiple(int value) const;
The first const applies to the return type of the member function.
The second const applies to the invisible this argument that is a pointer to the instance calling the Foo method.
First one is the type of the return value (const Foo), the second one means that it doesn't alter the class.
Related
This question already has answers here:
Passing references to pointers in C++
(10 answers)
Closed 4 years ago.
I have two function signatures in C++
void printArray(int* arrayPtr);
void printArray(int*& arrayPtr);
I understand the 1st function. It says the function takes in a arrayPtr argument which is of type that's a pointer pointing to an integer.
Both function signature works, but I have a hard time understanding the 2nd signature(*&) and what benefits it offers?
It's exactly the same as type versus type&; the first is a value and the second is a reference. The fact that type is a pointer doesn't change that.
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 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.
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