To add to what's already been said, when you make a method const, the object instance that it gets (the this
pointer) essentially becomes const. Keep in mind that when you're returning vec
, you're implicitly returning this->vec
:
return this->vec; // it's a const std::vector<ABC> since "this" is const
"Constness" can't be taken away -- unless you explicitly take it away with a const_cast<>
.
// to illustrate what's happening when you're returning from the function
std::vector<ABC> &return = this->vec; // can't assign const to non-const!
Therefore your return type also has to be const:
const std::vector<ABC> &return = this->vec; // all is good