I have a VS2008 C ++ program where I wrap a C API for use in a C ++ program. The C API expects an array of TABLE_ENTRY values, as shown below.
Besides copying data from each MyClass structure to TABLE_ENTRY's new structure MyClassCollection::GetTable(), is there a way to get the functionality I'm looking for?
Thanks PaulH
struct TABLE_ENTRY {
const char* description;
DWORD value;
};
class MyClass
{
public:
MyClass( const char* desc, DWORD value ) :
description( desc ),
some_value( 1 )
{
};
TABLE_ENTRY* GetTable()
{
entry_.description = description.c_str();
entry_.value = some_value;
return &entry_;
};
TABLE_ENTRY entry_;
std::string description;
DWORD some_value;
};
class MyClassCollection
{
public:
TABLE_ENTRY* GetTable()
{
return collection_.front()->GetTable();
};
void Add( MyClass* my_class )
{
collection_.push_back( my_class );
}
private:
std::vector< MyClass* > collection_;
};
int _tmain( int argc, _TCHAR* argv[] )
{
MyClass class1( "class1", 1 );
MyClass class2( "class2", 2 );
MyClassCollection collection;
collection.Add( &class1 );
collection.Add( &class2 );
TABLE_ENTRY* table = collection.GetTable();
TABLE_ENTRY entry1 = table[ 0 ];
TABLE_ENTRY entry2 = table[ 1 ];
return 0;
}
source
share