[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Overloading the ++ operator for a struct pointer

Posted on 2004-03-29
3
Medium Priority
?
481 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 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

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

873 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