I would like to overload the ++ operator for a pointer to a structure, so I can jump from one structure the the other.
Example code:
struct MyHeader
{
char length1;
char length2;
};
// &MyHeader + Length1 + Length2 + sizeof(MyHeader) = the memory adres where I can find the next header.
// example: 0x44444444 + 10 + 20 + 2 = 0x44444476
MyHeader *Header;
Header = (MyHeader)0x44444444; // some memory adres where there is a struct of the MyHeader kind.
// 0x44444444: 10
// 0x44444445: 20
// 0x44444476: 30
// 0x44444477: 40
Header->length1; // should be 10
Header++; // moving to the next header
Header->length1; // should be 30
So how can I overload the ++ operator for *MyHeader? Is this even possible?
No, you can't do exactly that, but you can do something like it. The usual approach is to define an iterator class and use iterators instead of pointers. Then you can overload operators in the iterator class to do what you want. I'll show you what a class declaration might look like to get you started.
MyHeader *Header = ...;
Header++; // adds sizeof(MyHeader) to Header