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
Solved

Overloading the ++ operator for a struct pointer

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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…

837 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