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