[SOLVED] variable naming in loop



  • Hello,

    assuming i have to create a bunch of objects from CustomClass.

    @CustomClass *item_1 = new CustomClass();
    CustomClass *item_2 = new CustomClass();
    ...
    CustomClass *item_99 = new CustomClass();@

    What is the usual way to perform this task in a loop ?

    @ for(i=0;i<100;i++) {
    CustomClass *item_?i? = new CustomClass();
    }

    @

    actually i use this approach but i' m not very happy with this:
    @ CustoClass *item[100];

    for(i=0;i<100;i++) {
        
        CustomClass *item[i] = new CustomClass();
    }
    

    @


  • Lifetime Qt Champion

    Hi,

    If you need that much instances of your class then using a container like QVector is the way to go.



  • @QList< CustomClass*> items;
    for (int var = 0; var < 100; ++var) {
    CustomClass * item = new CustomClass();
    items.append(item);
    }@



  • quick as chat this forum :)

    Yes, found code examples using QVector and QMap.
    I already use a QMap in CustomClass to gather and keep track on this objects.
    (you might remember yesterday, "Get all Objects with same class ?":http://qt-project.org/forums/viewthread/52664/

    Maybe there is a way to combine both needs to a single QMap ?


Log in to reply
 

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