Reputation: 427
I'm struggling to get my head around the differences between the various places you can put 'const' on a function declaration in c++.
What is the difference between const at the beginning:
const int MyClass::showName(string id){
...
}
And const at the end like:
int MyClass::showName(string id) const{
...
}
Also, what is the result of having const both at the beginning and at the end like this:
const int MyClass::showName(string id) const{
...
}
Upvotes: 30
Views: 17958
Reputation: 113
Questions like this tend to strengthen my resolve to follow the advice of Dan Saks as outlined in this "Conversations with a Guru" article: http://www.drdobbs.com/conversationsa-midsummer-nights-madness/184403835 .
In particular it deals with how the placement of const changes things. (*) I realize that I am extremely unlikely to convert anyone from writing const int ...
to int const ...
, but that said, there is one reason I prefer to do the latter.
(*) incuding a note that swapping the const with the type declaration at the very start is the one change that has no effect.
It makes for very easy readability, because for any instance of the word
const
in a declaration, everything to the left of it is the type of what is const, and everything to the right is what actually is const.
Consider a declaration like:
int const * * const pointerToPointer;
The first const
states that the integers at the end of the pointer chain are const, and they are to be found at * * const pointerToPointer
. Meanwhile the second one states that an object of type pointer to pointer to const int is also const and that this object is pointerToPointer
.
In the OP's case:
int const MyClass::showName(string id){
...
}
The type of what is const is int, and what is const is the return value from the function.
Meanwhile:
int MyClass::showName(string id) const {
...
}
Here the type of what is const is function(string) returning int, and what is const is the function itself, i.e. the function body.
Upvotes: 0
Reputation: 613531
const
attached to the return value applies to the return value. Since the return value is copied it's a pointless declaration and it makes no difference whether or not you include it.const
after the parameter list means that the function does not modify any state of the object that is not marked as mutable. This is a const member function and if you have a const object the compiler will not allow you to call non-const member functions on a const object.There is no interaction between these two uses of const
- they are completely independent constructs
Upvotes: 14
Reputation: 73503
const int MyClass::showName(string id)
returns a const int
object. So the calling code can not change the returned int. If the calling code is like const int a = m.showName("id"); a = 10;
then it will be marked as a compiler error. However, as noted by @David Heffernan below, since the integer is returned by copy the calling code is not obliged to use const int
. It can very well declare int
as the return type and modify it. Since the object is returned by copy, it doesn't make much sense to declare the return type as const int
.
int MyClass::showName(string id) const
tells that the method showName
is a const
member function. A const member function is the one which does not modify any member variables of the class (unless they are marked as mutable
). So if you have member variable int m_a
in class MyClass
and if you try to do m_a = 10;
inside showName
you will get a compiler error.
Third is the combination of the above two cases.
Upvotes: 41
Reputation: 437834
The difference is that the const
applies to different things.
This says that showName
returns a constant int
value -- one that is immutable. Of course since the int
is returned by value the presence of const
here does not play any role.
const int MyClass::showName(string id)
And this says that showName
does not modify the observable state of MyClass
(technically: it does not modify any member that is not declared mutable
), and therefore you are allowed to call it on a value of type const MyClass
.
int MyClass::showName(string id) const
If you use both const
s then both of the above apply.
Upvotes: 5