Michael Francis
Michael Francis

Reputation: 8747

Cannot assign to this

I have a class like so:

class Node {
    // data
    Node* next;
    void operator++(int);
};

And when I define the post-increment operator like so:

void Node::operator++(int) {
    this = this->next;
}

I get the error Expression is not assignable on this = this->next;.

What's up with this? How do I get this to point to next?

Upvotes: 3

Views: 1055

Answers (7)

jotik
jotik

Reputation: 17910

According to the C++ standard the this pointer is a prvalue expression (see [class.this]), however the assignment operator requires an lvalue as its left operand (see [expr.ass]).

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490338

Don't even try. It won't work, and if you could make it work, it would be a bad thing anyway.

From the looks of things, your node is in something like a linked list. If that's the case, then what you normally want is something like:

template <class T>
class linked_list { 

    class node { 
        node *next;
        T data;
    };

    node *head;
public:
    class iterator { 
        node *n;
    public:
        iterator(node *n=NULL) : n(n) {}

        iterator &operator++() { 
            n=n->next;
            return *this;
        }
        bool operator!=(iterator const &other) const { 
            return n != other.n;
        }
        // ...        
    };

    iterator begin() { return iterator(n); }
    iterator end()   { return iterator();  }
};

Upvotes: 2

Elemental
Elemental

Reputation: 7521

You seem very confused in terms of what you are trying to do here. You have a node class which represents a node in the list. Typically one would also have an iterator class which represents the position in the list that you are currently at. It seems to me that you are trying to use ++ to iterate to the next item in a list, but to do that you would need the iterator as a class of it's own.

In general it makes no sense to assign to the this pointer - this refers to the current object context, you can't just change that context by setting the this pointer.

I hope this will help you somewhat.

Upvotes: 0

Daniel Frey
Daniel Frey

Reputation: 56883

You just discovered why the STL has iterators. An iterator is holding a pointer to a node as a member of itself or in some cases it is a pointer. It could also modify it's member variable. Maybe try something like:

class Iterator {
    Node* node;
public:
    Iterator(Node* n)
        : node( n )
    {}

    Iterator& operator++()
    {
        node = node->next;
        return *this;
    }

    Iterator operator++(int)
    {
        Node* current = node;
        node = node->next;
        return Iterator(current);
    }

    Node& operator*()
    {
        return *node;
    }

    const Node& operator*() const; // etc...
};

Upvotes: 0

Bhupendra Pandey
Bhupendra Pandey

Reputation: 358

This can not be changed. it is constant.

Upvotes: 1

uba
uba

Reputation: 2031

The hidden parameter 'this' is passed with the 'const' qualifier. Hence it cannot be changed. Hidden this

Upvotes: -1

You cannot do that. Even if by some dirty tricks you change this, the change won't be propagated to the caller. this is a hidden formal argument of methods.

Upvotes: 0

Related Questions