?
Solved

Overloading the ++ operator for a struct pointer

Posted on 2004-03-29
3
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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