Global Object Pointer

I'm making a sort of system that will process every object in a list, and will use a function inherited by another class. Every object in this system will inherit this class. My question is this:

Is there any type of declarator or object that works globally with all objects? I'm using a single list here, and since this is supposed to be able to work just by adding an object to the stack (regardless of the object type), I can't just use a void pointer and try to cast it, since the object might be completely unknown. Would it be possible to use the class that each class would inherit?
So, you have a class A from which all the classes you want to add to the list derive, right? Remember that inheritance is an "is-a" relationship. This means that all classes derived from A are also As.
So you can just have the list take pointers to A and you can add to it pointers to objects of classes derived from A.
I don't remember ever doing this before, though, and my inheritance is rusty, so I'm not sure what you'd have to do to get the pointers back in their original type.
Alright, I glanced over the IS_A relationship in my book, but never completely understood it.

The way the system works is that a list will be created (From the STL), and every time an object is created, it would be added to the list. Then, when it's time to go and process each object in the list, it would iterate through the list and use the function inherited by the base class. These functions would only be prototyped by the base class, and then the user would define the function from within the class that's inheriting the base class.
What you are describing is exactly what inheritance is for.

If you want to make a global list, the parent class could have a static list member:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// parent.h

class Parent
{
public:
           Parent();  // default ctor  (adds this object to the list)
  virtual ~Parent();  // dtor (removes the object from the list)

  // function called to update all "registered" objects
  static void UpdateAll();

protected:
  // the pure virtual "Update" function to be implemented by
  //  all derived classes
  virtual void Update() = 0; 

private:
  // the list of "registered" objects
  static std::list<Parent*>  theList;
};


// parent.cpp

std::list<Parent*> Parent::theList; // instantiate the static list

// default ctor
Parent::Parent()
{
  // register this object
  theList.push_back(this);
}

// dtor
Parent::~Parent()
{
  // remove from list
  theList.remove(this);
}

// UpdateAll
void Parent::UpdateAll()
{
  std::list<Parent*>::iterator i;
  for(i = theList.begin(); i != theList.end(); ++i)  // update each object in the list
    i->Update();
}


Derived classes simply need to supply the 'Update' function, and have 'Parent' as one of their parents, and they're automatically registered and get updated by the UpdateAll call.


EDIT:
A word of warning with this approach. Do not have any global or static instances of Parent or any derived classes, as this would trigger the static initialization order fiasco. If you need global objects there is a work-around.


EDIT2:
You'd also need to register the objects in the copy ctor if object copying is allowed.


EDIT3:
To explain the "is a" bit...

A better name for "Parent" would probably be "UpdatingObject" or something similar. Since that is effectively what the class represents: an object that updates.

If you have a class Foo that's derived... then Foo "is an" Updating Object because it updates. Anything that you can do with an UpdatingObject you can do with a Foo, because a Foo is an UpdatingObject.
Last edited on
Alright, thanks, this solves another problem of mine. :D

Is it possible to still have the derived classes to have their own constructor, and still use the derived constructor? Or are they limited to that constructor?
Is it possible to still have the derived classes to have their own constructor, and still use the derived constructor? Or are they limited to that constructor?


Not only is it possible, it's mandatory. Derived classes must choose one parent ctor and call it. This is done automatically by the derived class (or you can do it explicitly in the derived class' initializer list).
Topic archived. No new replies allowed.