0

I have the following class

File Node.c

std :: vector<NodeConnection*>* Node :: GetConnections() const
{
    const std :: vector <NodeConnection*> * output = &this->connections;
    return output;
}

file Node.h

class Node {
    private:
        std :: vector <NodeConnection*> connections;
    public:
        std :: vector <NodeConnection*>* GetConnections() const;
};

I am trying to convert the vector connections to a const pointer. However, I keep getting the error

[Error] invalid conversion from 'const std::vector<NodeConnection*>*' to 'std::vector<NodeConnection*>*' [-fpremissive]

How would I go about converting it to a constant pointer that I can return?

1 Answer 1

2

It's because GetConnections is marked as const but returns a non-const pointer.

Instead you should return either by value, const pointer, or (my advice) by const reference:

const std::vector<NodeConnection*>& GetConnections() const;

Making a member function const means that it will not (and is in fact disallowed from) change any object member variables. When you return a non-const pointer, the caller could possibly change the returned value which conflicts with the constness of the function.

The line causing the error is the return, when you return a const pointer and the compiler has to convert it to a non-const pointer.

Sign up to request clarification or add additional context in comments.

1 Comment

The method template const std :: vector <NodeConnection*>* GetConnections() const; works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.