mardi 18 février 2020

Construct an object from multiple sources including a vector of objects

I get different fields [of potentially one class] from two different sources on the fly in c++. First a class A's fields gets passed on in function #1 (getData_Main) and then a vector of fields in another local function #2(getVecor_Sublass); The function calls are always sequential #1 followed by #2. Here are the simplified data structures;

class Subclass {
    string str;
    string str2;
    //a few other fields 
};

class Main {
    string name;
    float somevar;
    // few more string fields
    vector<Subclass> vecSubclass; 
};

I need to save the state of data to make it persistent to be used in other interfaces, something like construct a vector<Main*> object vector. I thought about creating a variable that serves as an index to main class and use the same index in Subclass and form a sort of lookup table? But then how to do that in best possible and more efficient way?

Unfortunately I'm not much familiar with the modern C++ (11/14) features and constructs, still a c++ 03 developer) so thought about posting here to get some suggestions with some working piece of code snippet in case of lambdas or any newer features?

class Main 
{
    int index; //create from some static variable in function A and pass on to function B 
    string name;
    float somevar;
    vector<A> vecA;
};

Aucun commentaire:

Enregistrer un commentaire