Advertisement

Arg

Started by January 02, 2001 11:04 PM
1 comment, last by Agent1 24 years ago
  
struct ObjNode
{
    Object object;
    ObjNode* next;
};

class Object
{
    ObjList* Children;
};
  
The above code is just to sort of clarify my question... not actual code Anyway, I''ve got a bunch of world/object/etc stuff. I want some classes to have a linked list in them, and this linked list might need an instance of the class. How can I do this? Is there a way to declare the class before using it in the struct? BTW - Don''t say anything about the code; it''s late and I just jotted it down
-Agent1
Ima Genius: *watches Agent compile the program, run it, and Agen''t cd rom tray opens and computer catches on fire*
Ima Genius: You''re an operator in a phone company and you have to connect all the calls right and if you do, Asheron calls you and tells you "Good job!" :D
If you declare it as a static object, e.g
Object object;
then the compiler has to know what it is, how big it is, etc. So you need to define the actual class before this.
However, if you declare it as a pointer, e.g:
Object *object;
Then the compiler only needs to know that the class exists, so you can tell it that by doing this:

class Object;

struct ObjNode
{
Object *object;
ObjNode *next;
};

.. then define Object somewhere else, and at runtime say object = new Object. Not entirely sure about this but I think if you define it somewhere else then new Object should work.
.. I hope this works
Advertisement
  class ObjNode{ class Object obj; ObjNode* next;};class Object{ ObjectList* children;};  


That works for me in MSVC6, anyway. (I think ming chokes on that, though)
You can also just prototype, by declaring it above the whole mess, like so:
class Object;

Of course, I tend to be wrong half the time, so chances are one of those two should work.

This topic is closed to new replies.

Advertisement