Link to home
Start Free TrialLog in
Avatar of generaldusty
generaldusty

asked on

Overloading the ++ operator for a struct pointer

Hi,

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?

sincerly,
Erik M.
Avatar of AlexFM
AlexFM

You don't need to do this, C++ compiler does this exactly as you want.

MyHeader *Header = ...;
Header++;                       // adds sizeof(MyHeader) to Header
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of generaldusty

ASKER

efn:
I was kinda hoping to avoid that solution, but if that's the only way so be it....

thnx for the quick reply, you get the point ;)

sincerly
Erik M.