const T & top() const { return m_Data.back(); }
It means that this
pointer in member function is const
. In other words that the call doesn't modify the object. (and whatever reference/pointer it returns will also be const
).
This syntax if for methods, inside classes. Methods marked as const
(the second const
in your code) can not modify the attributes of the object, only read. Const methods are the only callable methods if you instantiate your object as const
. For intance:
class A {
public:
void put(int v) {
var = v;
}
int read() const {
return var;
}
private:
int var;
}
int main() {
A obj;
obj.put(3);
const A obj2 = obj;
obj2.read(); // OK, returns 3;
obj2.put(4); // Compile time error!
}
Michael's answer covers just about everything, but there are some other aspects:
Only member functions can be const qualified, non-member functions can not. The same is true for C++ too. C has no concept of member functions and hence they can not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.