const class method problem

Mar 15, 2019 at 7:30pm
Hello Forum,

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.)

Thank you.
Mar 15, 2019 at 7:34pm
at a guess you forgot to cast the source pointer to const before trying to return it.
return (const vector<int>*)(&variable);
Mar 15, 2019 at 7:37pm
Why are you trying to return a pointer to the vector?

Mar 15, 2019 at 8:29pm
Hello,

Thank you for the replies.

I tried changing the return type and it works. But it doesn't make sense to me. I am changing it to a type that it already is (?).

1
2
3
4
5
6
7
8
9
10
// Header:
class MyClass
{
public:
	std::vector<int> * getVectorPointer() const;
private:
	std::vector<int> mVector;
};
// Source:
std::vector<int> * MyClass::getVectorPointer() const { return (std::vector<int> *) &mVector; }


Mar 15, 2019 at 8:37pm
But why are you returning a pointer instead of a copy of the vector? Normally in C++ you should strive to avoid pointers as much as possible.

1
2
3
4
5
6
7
8
9
10
// Header:
class MyClass
{
public:
	std::vector<int> getVector();
private:
	std::vector<int> mVector;
};
// Source:
std::vector<int> MyClass::getVectorPointer() const { return mVector; }


Mar 16, 2019 at 8:54am
Hello,

A pointer allows me to access the variable itself.
Passing a pointer is faster, since it does not have to copy memory.

For now it's just for testing purposes. It's probably best if the vector not accessable. Only the class itself is allowed to perform tasks with it.

Thanks.
Mar 16, 2019 at 11:57am

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.

Mar 16, 2019 at 4:12pm
A pointer allows ...

So does the reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Header:
class MyClass
{
public:
	const std::vector<int>& getVector() const;
private:
	std::vector<int> mVector;
};

// Source:
const std::vector<int>& MyClass::getVector() const
{ return mVector; }

// Use
const MyClass stone;
const std::vector<int>& sv = stone.getVector();
Topic archived. No new replies allowed.