Is it possible to access different objects (made by the same class) by substituting the name of the object in run time?



  • hey bros,
    I have a class or structure and i made some objects by this class(structure) as below:

    class myclass{
    int a;
    string b;
    float c;
    ...
    };
    myclass obj1,obj2,obj3,...,obj100; //making objects

    as you know i can use datamembers of all objects:
    obj1.a,
    obj1.b,
    obj1.c,
    ...

    i need a function or something like that to access all objects just by changing the name in run time, like:
    func(string str) { //str will be "obj1" or "obj2" or "obj3" or ...
    str.a=10;
    str.string="hello";
    ...
    }
    i know that a solution to define an array of objects and sweep on them by changing the index like:
    myclass obj [100];

    But i need to use names or string to substitute the object name.
    Thanks a lot.



  • If what you are looking for is "Access an object by name", you can use QHash or QMap, use a string as key and your object type as value.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.