Changing structure address in C ++ using ++ and - operator

Is it possible to change the address of my current structure using the operator - or ++, i.e.:

mystruct* test = existing_mystruct; test++ // instead of using: test = test->next_p; 

I tried to use this, but it seems const and gives me an error: assignment to this (anachronism):

 struct mystruct { mystruct* next_p; mystruct* prev_p; void operatorplusplus () { this = next_p; } void operatorminusminus() { this = prev_p; } }; 
+4
source share
2 answers

Objects have a constant address in memory as long as they exist. However, you can copy them to the new address.

What you are trying to do is promote in a linked list. And this can be done with these operators if you overload them. But you will need to determine what is in the special descriptor class to carry over the list nodes.

EDIT

The code for the description will look something like this:

 class mylist { struct mynode { //data mynode* next; mynode* prev; } *curr; public: mylist& operator++() {curr = curr->next; return *this;} }; 

Naturally, you would like to do border checks, etc., but this is a general idea.

+4
source

No. this pointer is of type mystruct * const , which means its address is unchanged.

+1
source

Source: https://habr.com/ru/post/1438052/


All Articles