I have a class that has a variable of type std::vector<int>.
The class has a method that returns the pointer to the std::vector<int> variable.
I want to declare the method as 'const', but it won't let me do it.
Why not? I am not modifying anything inside the method, just returning the pointer to the variable.
Error msg = "return value type does not match the function type".
(Without the 'const' keyword, everything is fine.)
A pointer allows me to access the variable itself.
But why do you want to give the outside world direct access to the variable itself?
Passing a pointer is faster, since it does not have to copy memory.
But then why make the vector private to the class, or even part of the class. Giving the outside world total control of the vector is usually a sign of a bad design.
For now it's just for testing purposes.
So then speed should not be a big problem. Just use a copy, then if you forget to remove the member function you won't inadvertently give up control of the variable.
It's probably best if the vector not accessable. Only the class itself is allowed to perform tasks with it.
This is correct. If you need direct access to the variable then you probably should look closer at the design of the class and remember that in C++ not everything must be part of some class. It is acceptable to have non class variables and functions.