this is what happens here:
1 2 3
|
const std::vector<int> getV() {
return this->v;
}
|
- function getV() returns a const qualified copy of the vector, why you decided to const qualify it
i don't get, unless you din't want the caller to modify the copy which isn't memory
efficient at all{what if the vec was a really big container}
- if you want the caller to read and not modify the vector, then the best option would be
1 2 3
|
const std::vector<int>& getV() { ///const reference, safe and mem efficient
return this->v;
}
|
here a reference to the vector is returned, guarded from modification and no unnecessary
copying is needed
ADVICE:
As @Repeater said when you return a copy, your class data remains safe.
Return a copy if you want the caller to modify it, here your class values
can't be changed and the caller can modify the copy, if you just just want
the caller to read and not modify your class values, return a const reference