Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] variable naming in loop
Qt 6.11 is out! See what's new in the release blog

[SOLVED] variable naming in loop

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    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();
    }
    

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        euchkatzl
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          deleted28
          wrote on last edited by
          #4

          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 ?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved