Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to create an array of data types only

Reply
Thread Tools

How to create an array of data types only

 
 
billy
Guest
Posts: n/a
 
      05-13-2004
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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-13-2004

"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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-13-2004
>
> 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


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-13-2004
"John Harrison" <(E-Mail Removed)> 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>.


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-13-2004
"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 *".


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      05-13-2004

"Siemel Naran" <(E-Mail Removed)> wrote in message
news:uXEoc.88394$(E-Mail Removed)...
> "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






 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-14-2004
"Howard" <(E-Mail Removed)> wrote in message news:etLoc.50811
> "Siemel Naran" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
iterative towers of hanoi using only °elementary° data types bagratte C Programming 16 02-22-2012 05:06 PM
equivalent c data types for vc++ data types ramu C Programming 2 02-20-2006 09:33 AM
char array to numeric data types - fastest conversion Eddy C Java 3 09-24-2005 03:13 AM
Doing things the hard way...accessing an array as different data types J. Campbell C++ 1 11-26-2003 06:39 PM



Advertisments