Solved

Overloading the ++ operator for a struct pointer

Posted on 2004-03-29
3
462 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 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

Technology Partners: 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 …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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