Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "*&" mean together? [duplicate]

Tags:

c++

pointers

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?

like image 364
Bek Avatar asked Aug 15 '26 21:08

Bek


2 Answers

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
like image 157
icktoofay Avatar answered Aug 17 '26 10:08

icktoofay


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"

like image 45
Adrian Shum Avatar answered Aug 17 '26 11:08

Adrian Shum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!