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
Related
This question already has an answer here:
About meaning of ::* on this code [duplicate]
(1 answer)
Closed 5 years ago.
While refactoring a piece of code I came across the line below:
class Bar
{
protected:
int (Bar::* fooFunction)(float); //this line
}
I have never seen this kind of syntax before. What is this syntax and why is it used it used in C++?
It's a member function pointer.
Specifically, it's a pointer to a member function of a Bar object that takes a float argument and returns a int.
Read more here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_member_functions
What is this syntax
It declares a pointer to member function.
why is it used it used in C++?
It is used to point to non-static member functions.
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:
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.
This question already has an answer here:
Why having const and non-const accessors?
(1 answer)
Closed 8 years ago.
So, I have to write two versions of the bracket operator for a class that I am writing.
Details:
The class has a private variable that is an array of strings.
The versions of operator[] differ in that one must be const, and the other not
I'm a little unsure how these two versions actually differ, and how when called, they will be able to be differentiated.
The one I currently have written
string& MyClass::operator[](unsigned int index);
simply returns the string at the index.
My question is, what is the point of having one const and one non-const? And how will they differ from each other?
EDIT:
Since I have to write these, will the code within each function be identical?
The correct overloaded versions are:
Access non const MyClass instance
std::string& MyClass::operator[](unsigned int index);
Access const or temp MyClass instance
const std::string& MyClass::operator[](unsigned int index) const;
The latter guarantees that access through a constant instance reference won't allow to modify the returned std::string reference, but to return a result though.
Implementation might need to consider that you could at least add new (default) string values to your container for currently unknown index values, and you'll need to decide how to handle unknown index values for const access (return empty string, throw exception, etc.).