How to create an array of data types only

B

billy

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
 
J

John Harrison

billy said:
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
 
J

John Harrison

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
 
S

Siemel Naran

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>.
 
S

Siemel Naran

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 *".
 
H

Howard

Siemel Naran said:
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
 
S

Siemel Naran

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top