#include 'iostream' #include 'vector' class ABC { }; class VecTest { std::vector<ABC> vec; public: std::vector<ABC> & getVec() const { //Here it errors out return vec; } };
Removing the const fixes it , is it not the case that getVec is a constant method. So why is this not allowed?
What you should probably be doing is returning a const reference.
const std::vector& getVec() const { return vec; }
It’s not allowed because you’ve said getVec is a const method, meaning the method should not change the this object in any way. Returning a non-const reference would possibly allow its object to be changed, so the compiler doesn’t allow it.