Solved

Overloading the ++ operator for a struct pointer

Posted on 2004-03-29
3
429 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:generaldusty
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10702354
You don't need to do this, C++ compiler does this exactly as you want.

MyHeader *Header = ...;
Header++;                       // adds sizeof(MyHeader) to Header
0
 
LVL 15

Accepted Solution

by:
efn earned 125 total points
ID: 10702394
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.

class MyHeaderIterator
{
public:
  MyHeaderIterator();
  MyHeaderIterator(MyHeader* inptr);
  MyHeader& operator *();
  MyHeader* operator ->();
  MyHeaderIterator& operator ++();  // Prefix
  MyHeaderIterator& operator ++(int);  // Postfix
private:
  MyHeader* ptr;
};

--efn
0
 

Author Comment

by:generaldusty
ID: 10702814
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now