const T& data() const { return data_; }
^^^^^
means it will return a const
reference to T
(here data_
)
Class c;
T& t = c.data() // Not allowed.
const T& tc = c.data() // OK.
const T& data() const { return data_; }
^^^^^
means the function will not modify any member variables of the class (unless the member is mutable
).
void Class::data() const {
this->data_ = ...; // is not allowed here since data() is const (unless 'data_' is mutable)
this->anything = ... // Not allowed unless the thing is 'mutable'
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…