Registering sub-classes to a central location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MTsoul
    New Member
    • Nov 2008
    • 7

    Registering sub-classes to a central location

    Here is an interesting problem I've come across. It's more on the use of some C++ design patterns.

    Scenario: There is an interface. Sub-classes implement this interface. There is a manager that should iterate through these sub-classes and issue commands, etc.

    Problem: How is it possible to register these sub-classes to the manager at compile time?

    I have tried doing something like:

    Code:
    typedef struct List {
        Interface * subclass;
    } List;
    List classes[] = {
        new Subclass1();
        new Subclass2();
    };
    and let the manager iterate through classes. However, this means the manager will have no control over which subclass gets loaded into memory. How is possible to address a class without instantiating it? I realize this is kind of like reflective programming, and I don't mind templates in this case.

    Thanks.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Why would you need to register these class with the manager, surely the manager can just access them through the interface class.

    Comment

    • MTsoul
      New Member
      • Nov 2008
      • 7

      #3
      The question is how? The interface is great if I have an object of the subclass, in which case the manager can manipulate the object with the interface. How do I address the subclass if no objects have been created?

      Code execution starts from the manager, which should then instantiate a bunch of subclasses (through the interface or some other mechanism). How can this be done without statically instantiating the subclasses like the code in the first post?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I think you may wish to look up the factory design pattern which does this sort of thing.

        Comment

        • MTsoul
          New Member
          • Nov 2008
          • 7

          #5
          Thanks. I thought about that but decided to go with function pointers. Factory classes have the same problem as the one I described. I still need to instantiate the Factory classes to make new objects. Function pointers take care of that problem I think.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by MTSoul
            How is possible to address a class without instantiating it? I realize this is kind of like reflective programming,
            You can't. Unless there's an object there's no address and there's no object until your code executes. Ergo, nothing can be done at compile time.

            Do as Banfa suggests and create a Factory. Your factory object cna be created on the heap and after it creates and registers your objects, it can be deleted.

            Comment

            Working...