Peter Olcott wrote:
I am not sure that I understand what you are saying.
There does not seem to be enbough detail to your
specification for me to infer exactly what you are proposing.
As I said: A base class and derived classes, which implement
the different data types you need to store.
Something like that (Warning: untested code)
class Value
{
public:
~Value() {}
virtual void PrintTo( std::ostream& To, int indent ) {}
};
class IntValue : public Value
{
public:
IntValue( int x ) : m_Value( x ) {}
virtual void PrintTo( std::ostream& To, int indent )
{ To << std::string( indent, ' ' )
<< "An int: " << m_Value << "\n"; }
protected:
int m_Value;
};
class DoubleValue : public Value
{
public:
DoubleValue( double x ) : m_Value( x ) {}
virtual void PrintTo( std::ostream& To, int indent )
{ To << std::string( indent, ' ' )
<< "A double: " << m_Value << "\n"; }
protected:
double m_Value;
};
class StringValue : public Value
{
public:
StringValue( std::string x ) : m_Value( x ) {}
virtual void PrintTo( std::ostream& To, int indent )
{ To << std::string( indent, ' ' )
<< "A string: " << m_Value << "\n"; }
protected:
std::string m_Value;
};
class ArrayValue : public Value
{
public:
~ArrayValue() { for( int i = 0; i < m_Value.size(); ++i )
delete m_Value[i]; }
virtual void PrintTo( std::ostream& To, int indent )
{ To << std::string( indent, ' ' )
<< "An array consisting of:\n";
for( int i = 0; i < m_Value.size(); ++i )
m_Value[i]->PrintTo( To, indent + 2 );
}
void Add( Value* Next ) { m_Value.push_back( Next ); }
protected:
std::vector< Value* > m_Value;
};
class Record : public ArrayValue
{
public:
void PrintTo( std::ostream& To )
{
To << "This record consists of:\n";
for( int i = 0; i < m_Value.size(); ++i )
m_Value[i]->PrintTo( To, 2 );
}
};
int main()
{
// Build up a structure
Record MyRecord;
MyRecord.Add( new IntValue( 5 ) );
MyRecord.Add( new DoubleValue( 2.0 ) );
MyRecord.Add( new StringValue( "Heureka" ) );
ArrayValue* pTemp = new ArrayValue;
pTemp->Add( new DoubleValue( 3.0 ) );
pTemp->Add( new DoubleValue( 1.0 ) );
pTemp->Add( new StringValue( "got it?" ) );
MyRecord.Add( pTemp );
MyRecord.PrintTo( std::cout );
return 0;
}
--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email:
kb******@gascad.at Web:
www.gascad.com
Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5