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?
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
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With