How to dynamically create a structure

A

AlabiChin

Hello,

I'm trying to find out if it is possible to dynamically add or remove
fields for a structure. I'm in a situation where I don't know how many
items I want to store and, therefore, will not know the size of the
structure at compile time and would like to handle it at runtime
(after having gathered necessary data).

If this is not possible to do this using standard C++, do you know if
it is in a Win32 API environment?

Thanks for any assistance with this?
 
V

Victor Bazarov

AlabiChin said:
I'm trying to find out if it is possible to dynamically add or remove
fields for a structure.

I'll tell you right now -- not possible. "Fields" (we use the term
'members') are set at compile-time and cannot be changed.
I'm in a situation where I don't know how many
items I want to store and, therefore, will not know the size of the
structure at compile time and would like to handle it at runtime
(after having gathered necessary data).

If this is not possible to do this using standard C++, do you know if
it is in a Win32 API environment?

Ask in a Win32 programming newsgroup.
Thanks for any assistance with this?

You're welcome?

V
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hello,

I'm trying to find out if it is possible to dynamically add or remove
fields for a structure. I'm in a situation where I don't know how many
items I want to store and, therefore, will not know the size of the
structure at compile time and would like to handle it at runtime
(after having gathered necessary data).

As Victor Bazarov said, it's impossible, but if they type for all the
members you'd like to add is the same or limited to a few types you
can use standard containers such as std::vector or std::map to store
the values in. There are also some boost containers which can be used
to store values of different types.
 
V

verdverm

Hello,

I'm trying to find out if it is possible to dynamically add or remove
fields for a structure. I'm in a situation where I don't know how many
items I want to store and, therefore, will not know the size of the
structure at compile time and would like to handle it at runtime
(after having gathered necessary data).

If this is not possible to do this using standard C++, do you know if
it is in a Win32 API environment?

Thanks for any assistance with this?

i disagee, it believe it is possible, i have done some my self, you
can check out Loki::typelist from the Modern C++ Design Book

What kind of "fields" or members, are they containers of, what are the
functions, methods, and use cases for them?

you can create a base class group and inheirt from them
 
I

Ian Collins

verdverm said:
i disagee, it believe it is possible, i have done some my self, you
can check out Loki::typelist from the Modern C++ Design Book
Typelists are built at compile time.
 
J

Jim Langston

AlabiChin said:
Hello,

I'm trying to find out if it is possible to dynamically add or remove
fields for a structure. I'm in a situation where I don't know how many
items I want to store and, therefore, will not know the size of the
structure at compile time and would like to handle it at runtime
(after having gathered necessary data).

If this is not possible to do this using standard C++, do you know if
it is in a Win32 API environment?

Thanks for any assistance with this?

I've run into that issue when dealing with interfacing with databases.
Basically I had to build my own structure class, so to speak, for my
records. In my scenario, all the data is stored in std::strings because,
well, that's the way they're stored in the MySQL database, but I could of
also stored ints into std::strings by copying the bytes back and forth.

I think it really depends on just what it is you are trying to do.

What you are looking for are generally called Fields, and C++ does not have
them built in. You either have to roll them your own or get some code from
someone who's already done it similar to how you need it.
 
J

James Kanze

I've run into that issue when dealing with interfacing with databases.
Basically I had to build my own structure class, so to speak, for my
records. In my scenario, all the data is stored in std::strings because,
well, that's the way they're stored in the MySQL database, but I could of
also stored ints into std::strings by copying the bytes back and forth.
I think it really depends on just what it is you are trying to do.
What you are looking for are generally called Fields, and C++ does not have
them built in. You either have to roll them your own or get some code from
someone who's already done it similar to how you need it.

In many cases, std::map will fit the bill just fine. The most
generic solution would be std::map< std::string, boost::any >,
but most of the time, you really don't want to be that
unrestrcited: either boost::variant or a Field* (with derived
classes for each of the target types) would be better for the
second type. And as you say, often, std::string is sufficient;
you can represent just about any type in a sring.
 

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,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top