Create a dynamic datastructure from mysql in qt C++ -


my aim of project display , perform computations comparing on data mysql database. mysql database contains many tables , first row in each table contains names of field such id, name, positionx, positiony, backgroundcolor , values in lower row.

what need create dynamic datastructure in c++/ qt name of variables same name of field. can store values based on chosen table

so when divide problem smaller steps
1. identify datatype based on values.
2. generate name of variables same names in row of mysql table.
3. create dynamic struct

example mysql table containing fields

id positionx positiony backgroundcolor
0x32 233 256 0x3366

i can create static datastructure

struct table { int id,positionx,positiony,backgroundcolor; }mytable; 

can generate struct dynamically?

ps:- (i have searched bit , have encountered term called reflection not understand have moved computer science (2 years), not know if in right path). sample code should me better rather theories.

you need understand theory first, sorry, otherwise nothing of make sense you.

a c++ compiler performs name-erasure , lots of type-erasure. means once compiler has compiled code, object storage nameless , typeless. types of structures virtual methods known - nothing known members. struct concept exists while code being compiled. concept ceases exist compiler done.

talking of "dynamic" struct makes no sense in c++, because @ runtime object code knows nothing "struct" is.

you can't reuse compile-time struct concept @ runtime, must design own instead. since you're using qt, can represent runtime-generated structure map names variants: qmap<qstring, qvariant>. qvariant can store type known qt's metatype system, , can let system know custom types might using too.

your table follows, then:

using dstruct = qmap<qstring, qvariant>;  // create prototype row dstruct mytable; mytable.insert("id", qvariant{}); mytable.insert("positionx", qvariant{}); mytable.insert("positiony", qvariant{}); mytable.insert("backgroundcolor", qvariant{});  // copy prototype row , initialize dstruct myrow = mytable; myrow["id"] = 5; myrow["positionx"] = 12; ... 

Comments