By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

How to create an array of data types only

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"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
Jul 22 '05 #2

P: n/a
>
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
Jul 22 '05 #3

P: n/a
"John Harrison" <jo*************@hotmail.com> 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>.
Jul 22 '05 #4

P: n/a
"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 *".
Jul 22 '05 #5

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:uX*********************@bgtnsc04-news.ops.worldnet.att.net...
"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


Jul 22 '05 #6

P: n/a
"Howard" <al*****@hotmail.com> wrote in message news:etLoc.50811
"Siemel Naran" <Si*********@REMOVE.att.net> 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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.