3

For example:

private:
    Node* curr;
public:
    Node*& Iterator::getCurr() {
        return curr;
    }

    bool Iterator::operator==(const Iterator& other) {
        return curr == other.getCurr();
    }

I'm getting error in this code:

passing ‘const Iterator’ as ‘this’ argument of ‘Node*& Iterator::getCurr()’ discards qualifiers [-fpermissive]

How should I fix it?

Bek
  • 45
  • 8

3 Answers3

2

don't read them together

if you see something like this:

Foo& foo();

Do you know what does the & means?

It is a reference to Foo

Then

Foo* foo();

What about this? this is Pointer to Foo

Then

Foo*& foo();

is reference to "Pointer to Foo"

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
2

Node*& means “reference to pointer to Node”. You can return it as normal. Accessing it, however, can be done two ways: the ‘normal’ way, where the “reference to” part will just be dropped, and the way preserving the reference. The advantage of the latter way is you can change the underlying curr value:

Node *&curr = iterator.getCurr();
curr = new Node();  // or something like that
// iterator.curr has been changed
icktoofay
  • 126,289
  • 21
  • 250
  • 231
0

It's a reference to a pointer to Node.

Ed S.
  • 122,712
  • 22
  • 185
  • 265