How to create an array of data types only

Discussion in 'C++' started by billy, May 13, 2004.

  1. billy

    billy Guest

    I've got a set of subclasses that each derive from a common base class. What
    I'd
    like to do is create a global array of the class types (or, class names)
    that a manager
    class can walk through in its constructor and instantiate one of each of the
    class types
    in this global array. So, it's almost like the global array has to hold data
    types as
    opposed to data. Basically, I currently have to add the items manually 1 at
    a time.
    I want to have the class names in a global array that I can walk through and
    turn the
    following code into a loop up to N_ELEMS(array) to avoid having to add one
    of
    these calls everytime I want to create a new OneOfTheObjectsIWantToAdd_N.
    Also note that I don't have control over the addItem() member.


    m_manager->addItem(
    new SomeNameSpace::SomeClass<OneOfTheObjectsIWantToAdd_1>(
    "Name Of The Object I Want To Add",
    &OneOfTheObjectsIWantToAdd_1::MemberMethod));

    m_manager->addItem(
    new SomeNameSpace::SomeClass<OneOfTheObjectsIWantToAdd_2>(
    "Name Of The Object I Want To Add",
    &OneOfTheObjectsIWantToAdd_2::MemberMethod));

    If I had an array of OneOfTheObjectsIWantToAdd_1 - _N, I think I could get
    away with
    putting the above code in a loop. The bottom line is that it is sort of like
    building an array
    of structure names and trying to malloc them at runtime based on their
    names. Anyway,
    thanks for any help.

    ~billy
    billy, May 13, 2004
    #1
    1. Advertising

  2. "billy" <billy_dev#@#cox#.#net> wrote in message
    news:zDDoc.4148$2c7.587@fed1read07...
    > I've got a set of subclasses that each derive from a common base class.

    What
    > I'd
    > like to do is create a global array of the class types (or, class names)
    > that a manager
    > class can walk through in its constructor and instantiate one of each of

    the
    > class types
    > in this global array. So, it's almost like the global array has to hold

    data
    > types as
    > opposed to data.


    You cannot create an array of types, but you can create a list of types.
    Here's some code to illustrate the idea.

    In the code below MyTypeList is the list of types and I recursively loop
    through it using the print_type_names template function.

    #include <iostream>
    using namespace std;

    template <class T, class U>
    struct TypeList
    {
    typedef T Head;
    typedef U Tail;
    };

    struct NullType
    {
    };

    #define TYPELIST_0() NullType
    #define TYPELIST_1(A) TypeList<A, TYPELIST_0() >
    #define TYPELIST_2(A, B) TypeList<A, TYPELIST_1(B) >
    #define TYPELIST_3(A, B, C) TypeList<A, TYPELIST_2(B, C) >
    #define TYPELIST_4(A, B, C, D) TypeList<A, TYPELIST_3(B, C, D) >
    #define TYPELIST_5(A, B, C, D, E) TypeList<A, TYPELIST_4(B, C, D, E) >

    struct SomeType {};
    struct AnotherType {};
    struct YetAnotherType {};

    typedef TYPELIST_5(int, SomeType, double, AnotherType, YetAnotherType)
    MyTypeList;

    template <class TL>
    void print_type_names()
    {
    cout << typeid(TL::Head).name() << '\n';
    print_type_names<TL::Tail>();
    }

    template <>
    void print_type_names<NullType>()
    {
    }

    int main()
    {
    print_type_names<MyTypeList>();
    }

    On my computer this prints

    int
    SomeType
    double
    AnotherType
    YetAnotherType

    yours may differ because the output of typeid(...).name() is implementation
    dependent.

    This type list idea was popularised by Alexei Alexandrescu in his book
    Modern C++ Design, if you are interested.

    john
    John Harrison, May 13, 2004
    #2
    1. Advertising

  3. >
    > template <class TL>
    > void print_type_names()
    > {
    > cout << typeid(TL::Head).name() << '\n';
    > print_type_names<TL::Tail>();
    > }
    >


    Oops, that really should be

    cout << typeid(typename TL::Head).name() << '\n';
    print_type_names<typename TL::Tail>();

    my compiler let me omit typename but it should be there really.

    john
    John Harrison, May 13, 2004
    #3
  4. billy

    Siemel Naran Guest

    "John Harrison" <> wrote in message
    news:2gggm2F2j71cU1@uni-

    > template <class TL>
    > void print_type_names()
    > {
    > cout << typeid(TL::Head).name() << '\n';
    > print_type_names<TL::Tail>();


    The compiler does not know whether TL::Tail is the name of a type of
    variable (it does not look at the context of where the variable is used
    which assures us that it must be a type), so it should be <typename
    TL::Tail>.
    Siemel Naran, May 13, 2004
    #4
  5. billy

    Siemel Naran Guest

    "billy" <billy_dev#@#cox#.#net> wrote in message
    news:zDDoc.4148$2c7.587@fed1read07...

    > I've got a set of subclasses that each derive from a common base class.

    What
    > I'd
    > like to do is create a global array of the class types (or, class names)
    > that a manager
    > class can walk through in its constructor and instantiate one of each of

    the
    > class types
    > in this global array. So, it's almost like the global array has to hold

    data

    Look up the factory pattern. It lets you feed in a string "matrix" and
    returns a pointer to a Driver, but it's really a MatrixDriver object, where
    MatrixDriver derives from Matrix. You can modify your factory to find all
    elements "M*" or just all elements, and return a new Driver for each. I did
    this for my test driver. You can run an individual test with "test matrix"
    or all of them with "test *".
    Siemel Naran, May 13, 2004
    #5
  6. billy

    Howard Guest

    "Siemel Naran" <> wrote in message
    news:uXEoc.88394$...
    > "billy" <billy_dev#@#cox#.#net> wrote in message
    > news:zDDoc.4148$2c7.587@fed1read07...
    >
    > > I've got a set of subclasses that each derive from a common base class.

    > What
    > > I'd
    > > like to do is create a global array of the class types (or, class names)
    > > that a manager
    > > class can walk through in its constructor and instantiate one of each of

    > the
    > > class types
    > > in this global array. So, it's almost like the global array has to hold

    > data
    >
    > Look up the factory pattern. It lets you feed in a string "matrix" and
    > returns a pointer to a Driver, but it's really a MatrixDriver object,

    where
    > MatrixDriver derives from Matrix. You can modify your factory to find all
    > elements "M*" or just all elements, and return a new Driver for each. I

    did
    > this for my test driver. You can run an individual test with "test

    matrix"
    > or all of them with "test *".
    >
    >


    Huh?

    You must be referring to some specific example that demonstrates the use of
    the factory pattern, right? The factory pattern itself has nothing to do
    with what you've just described (specifically: "matrix", Driver,
    MatrixDriver, "M *", "test matrix", and "test *"). It's a good solution,
    but all this mumbo-jumbo would confuse the heck out of anyone trying to
    glean what the factory pattern is or how it might relate to the original
    question.

    A less confusing answer might have been simply "Look up the factory
    pattern."

    -Howard
    Howard, May 13, 2004
    #6
  7. billy

    Siemel Naran Guest

    "Howard" <> wrote in message news:etLoc.50811
    > "Siemel Naran" <> wrote in message


    > > Look up the factory pattern. It lets you feed in a string "matrix" and
    > > returns a pointer to a Driver, but it's really a MatrixDriver object,


    > Huh?
    >
    > You must be referring to some specific example that demonstrates the use

    of
    > the factory pattern, right? The factory pattern itself has nothing to do
    > with what you've just described (specifically: "matrix", Driver,
    > MatrixDriver, "M *", "test matrix", and "test *"). It's a good solution,
    > but all this mumbo-jumbo would confuse the heck out of anyone trying to
    > glean what the factory pattern is or how it might relate to the original
    > question.
    >
    > A less confusing answer might have been simply "Look up the factory
    > pattern."


    Right, I proceeded with a specific example of the factory solution, but
    reading it over, it could be confusing, and doesn't do justice to the
    generality of the factory idea.
    Siemel Naran, May 14, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    587
    Sathyaish
    May 22, 2005
  2. ramu
    Replies:
    2
    Views:
    323
    rlblaster
    Feb 20, 2006
  3. bagratte
    Replies:
    16
    Views:
    1,520
    Liviu
    Feb 22, 2012
  4. Gary Roach
    Replies:
    0
    Views:
    111
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    120
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page