Is the order of method return types and modifiers in C++ important?
I have come across a situation that conflicts with my current understanding of methods in C++.
I am working through Ivor Horton's "Beginning Visual C++ 2010" (Wrox Press). On page 449, Example 8_03, a method is defined as:
double Volume() const {
return m_Length * m_Width * m_Height;
}
I had rearranged the modifiers as:
double **const** Volume() {
return m_Length * m_Width * m_Height;
}
From my C# and Java background, I had expected the position of const to be irrelevant, but on com开发者_C百科pilation I received the error:
error C2662: 'CBox::Volume' : cannot convert 'this' pointer from
'const CBox' to 'CBox &'
The error disappears when I return the order to the way Ivor has it.
Does the order in fact make a difference and this not some exotic bug ? If order does matter, how does one remember the correct positions ?
Thanks,
Scott
When const
is placed after the name of a member method, that is stating that the this
pointer is what is constant. That is to say, the original declaration states that the method CBox::Volume()
does not change the CBox
object on which it is called.
The most likely source of error is that the CBox::Volume()
function is being called on a const CBox
, or inside another const
method of that CBox
.
In this case, a const
after the member function name means the function itself is const
--basically, it means you cannot modify the object within the member function. The const before the member function means the return type is const
. (Though it can get complicated for return types, and I don't want to get into that here :) )
The const
after the method name tells the compiler that the method will not modify any data members (mutable
members are an exception).
The const
before the method name applies to the return value. This tells the compiler that the method returns constant data. The const
for the return type is primarily used with references and pointers.
Edited To be more concise.
const
in those two contexts is very different.
double Volume() const {
return m_Length * m_Width * m_Height;
}
This is called when the instantiation of the object is const
and returns a double
. If you were to try and call that method with a non-const instance, it will not compile.
const myClass obj;
double d = obj.volume();
myClass obj2;
double d = obj2.volume(); // This Fails to compile
On the other hand ...
double const Volume() {
return m_Length * m_Width * m_Height;
}
is called when the object isn't a const
or if the previous definition doesn't exist.
myClass obj;
const double d = obj.volume();
const myClass obj2;
const double d = obj2.volume();
(I should mention as I was reminded below, returning a const built-in type such as double
it doesn't really mean anything.)
When you have both of these declared the appropriate one will be called depending if the instance is const or not.
myClass obj;
const double d = obj.volume(); // This calls `const double volume()`
const myClass obj2;
double d = obj2.volume(); // This calls 'double volume() const`
Yes the order is important.
In C++ the position of the const keyword will have an impact on what item is "const". The same goes for the scope the const keyword is used in.
精彩评论