Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with dinamic lists

Status
Not open for further replies.

Dijkstra30

Programmer
Oct 24, 2005
13
RO
until now my dinamic lists were defined as follows:

typedef struct obj {
int info;
struct obj *left, *right;
};

the pointers *left, *right point to objects of the same type.
Now... I want to create a class of these kind of objects

Let's say this class look like this (if i'm not mistaken)

class Obj
{
int inf;
Obj *left, *right;
};


With this declaration I have problems accesing the next element in list;
I also have trouble with redefinig operators.
 
Without seeing how you're trying to use it or what errors you're getting, my first guess is that you're trying to use it just like a struct.
By default, all data in a struct is public, but in a class all data is private by default.
Try this:
Code:
class Obj
{
public:
  int inf;
  Obj *left, *right;
};
Although this is a very BAD way to implement it since all your data is exposed.
A better way would be something like this:
Code:
class List
{
public:
  List();   // Constructor.
  ~List();  // Destructor.

  void Insert( int value );  // Insert a new List node after this one.
  void Remove();  // Remove this List node.

  int   operator*();  // Get the data from this List node.

  List& operator++();  // Move to the next List node.
  List& operator++( int );

  List& operator--();  // Move to the previous List node.
  List& operator--( int );

private:
  int    m_Data;
  List*  m_Previous;
  List*  m_Next;
};
and once you do that you might as well just use the STL list template which does it a lot better than I can do by myself. ;)
 
It's the C++ language, folks.
You may add all members (ctor, dtor etc) to struct directly. A structure is a class with all public members in C++...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top