vector of template class question

M

mast2as

Hi there,

Here is the conceptual problem i try to find an elegant solution to. I
have a template class that I use to save data from a file. The data can
be integer, float, double. This is a skeleton code to show precisely
what i am talking about.

template<class T>
class Channel {
public:
Channel( int size )
{
data = new T[ size ];
}
~Channel()
{
if ( data != NULL )
{
delete [] data;
}
}
string name;
T *data;
};

I have an object which has a collection of such data. It can have 10
floats which are the data for a channel named A, 32 integers which are
the data of a channel named B, 14 doubles (channel D), and 42 integers
which are the data for a channel named E. Of course i can always do

Channel<float> A( 10 );
Channel<int> B( 32 );
Channel<double> C( 14 );
Channel<int> E( 42 );

The problem is that i read the data from a data file and the number of
channels as well as the number of data per channel is known after I have
read the header of the data file. So ideally the system I would like is
some sort of vector that i can use to create channels on the fly. But I
don't know if it possible to create a vector made of the class Channel
for which the type can be different. It is possible to write

vector<Channel <class T> > vecOfChannels;

as long as all the channels in vecOfChannels have the same type !

So is there a way I can do the same thing but where the channels in
vecOfChannels would have different type. Like in pseudo-code

vector<Channel <class T> > vecOfChannels.

Channel<float> A( 10 );
vecOfChannels.push_back( A );
Channel<int> B( 32 );
vecOfChannels.push_back( B );

I hope it's clear. Thank yoy very much for reading the message and help
me out. I have thought of this quite a while so any suggestion of idea
is really welcome.

Thank you -mast
 
A

alexmdac

All the elements of a vector have to be of the same type. However, if
there's something in particular that you'd like to do with the data
you've read, you could derive each container from a common abstract
base class and store pointers to this base class in the vector.

For example, if you'd like to read the data and then print it on a
screen, you might make an abstract class like this:

class ChannelBase
{
public:
virtual void Print( void ) = 0;
};

Then you'd make your channel template like this:

template< typename T > class Channel : public ChannelBase
{
public:
void Print( void ) { /* ... */ }
};

Now during reading you can populate a vector defined like this:

vector< ChannelBase * > readChannels;

To print all the data, you'd iterate through the vector (using
std::for_each perhaps) calling ChannelBase::print for each element.
Hope this helps.
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top