mercredi 27 mai 2020

How to assign values to the data elements of a structure

I have a structure

struct MyStruct
{
    int intValue1;
    float floatValue2;
    std::string stringValue3;
} Structure;

Now based on the value input of two strings, I want to assign values to the data elements of the structure:

std::string varName = "intValue1";
std::string varValue = "5";

So based on the two strings the "intValue1" should get a value of 5

Structure.intValue1 = (int)varValue;

Is it possible to write a function which would automatically assign values to the structure based on the input strings, e.g.:

 void SetData( std::string varName , std::string varValue );

Aucun commentaire:

Enregistrer un commentaire