Results 1 to 5 of 5

Thread: for i = 0 to nr members in a class ...

  1. #1
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default for i = 0 to nr members in a class ...

    I want to intialize a certain type of element (a class called *TMMap) that I have in my main class TWorld. There are a lot of these elements and they all have different names. I need all of them to be created and initisalized and to a certain value. This is done in a function NewMap(0).
    Can I find all members of a class of a certain type?
    something like:
    Qt Code:
    1. for (i = 0; i < nr of members in class TWorld; i++)
    2. if (member is of type TMMap)
    3. TMMap *T = (typecasting TMMap) member(i);
    4. T = NewMap(0);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: for i = 0 to nr members in a class ...

    Do TWorld and TMMap inherit QObject?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: for i = 0 to nr members in a class ...

    I need all of them to be created and initisalized and to a certain value.
    That can be done in the constructor of the class,,, isnt it ?

    If you want new objects to be initialized to a certain value, then -
    Qt Code:
    1. QList<TMMap *> tmmapList;
    2. for(index =0; index < numberOfObjectsToBeCreated; index++)
    3. {
    4. TMMap * map = new TMMap(); // Do the intialization in the ctor.
    5. tmmapList.append(map);
    6. }
    To copy to clipboard, switch view to plain text mode 

    If you want already created objects to be set to some defalt value then -
    Qt Code:
    1. QList<TMMap *> tmmapList; // already populated.
    2. for(index =0; index < tmmapList.count() ; index++)
    3. {
    4. TMMap * map = tmmapList.at(index);
    5. map->setToDefaultValues(); // Provide this function in the class
    6. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: for i = 0 to nr members in a class ...

    TWorld depends on object (it is a thread actually), tmmap not (but it could be).

    sorry for not being so clear
    thanks, well they can't be initialized in the contrustor because they have a **double structure with rows and cols that depends on the user database. so that is done at run time. sorry for not telling that.
    I understand you can make a list of pointers, but then each map is refernced by a number instead of its name, which has a meaning to understand the code. If all variables were referenced as a list number I would get completely lost in the code (its a waterbalance/soil erosion model using grid maps and the maps have names like discharge, vvelocity etc.). If I use numbers the code would become unreadable. These are my variables now:
    Qt Code:
    1. TMMap *tm, *Mask, *DEM, *DX, *Grad, *LDD, *Outlet, *RainZone, *N, *RR, *MDS,
    2. *Rain, *RainCum, *RainNet, *RainIntensity, *RainM3, *CStor, *Interc,
    3. *WH, *WHinf, *WHroad, *WHrunoff, *WHstore, *WaterVol, *WaterVolRunoff, *InfilVolKinWave, *InfilVol, *fpa,
    4. *FlowWidth, *V, *Alpha, *Q, *Qoutflow, *Qn, *Qs, *Qsn, *q, *R, *Perim, *WheelWidthDX, *StoneWidthDX,
    5. *SoilWidthDX, *GullyWidthDX, *RoadWidthDX, *WheelWidth, *StoneFraction, *CompactFraction, *CrustFraction,
    6. etc etc
    To copy to clipboard, switch view to plain text mode 
    can I use Qmetaobject somehow? thanks
    Last edited by qt_gotcha; 6th March 2010 at 17:30.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: for i = 0 to nr members in a class ...

    You could use numbers and then use enumerations to give those numbers names. Then you refer by name and the compiler converts to number at runtime.

Similar Threads

  1. Class value members & references
    By agnus in forum General Programming
    Replies: 3
    Last Post: 6th December 2009, 14:18
  2. Replies: 5
    Last Post: 29th November 2009, 15:09
  3. Question about class members
    By serenti in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2009, 14:20
  4. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.