c++ - Holding reference types in value typed list? -


in discussion what , stack , heap? learned value types kept @ stack , pointers @ heap.in application have qlist<t*> , qmap<qstring,qlist<qstring>> holding pointers of objects.

now know, reference types holding @ heap (application). lists, holding references, value types , @ stack. available in thread in created. how know, when thread dead, or if function/method, call in thread? need lists in whole application. bit stupid question fill lists , mere chance if objects available.

edit:

@coyotte508

the things in stack member pointers pointing memory. , is, if created in stack, if they're local variables of function or of class declared on stack.

in main class have list: qlist<fachbereich *> lfbs;

in constructor of main class create list:

lfbs=qlist<fachbereich *> (); 

..- call method readconfig();

one of subroutines :

void appconfig::readfachbereiche(qxmlstreamreader &reader) {     fachbereich* fb(0);     while(!reader.atend())     {         reader.readnext();         if (reader.error())         {             bla bla          }         else         {             if (reader.isstartelement())             {                 if (reader.name().tostring().tolower() == "fb")                 {                     qstring sfb="";                      try{ sfb=reader.attributes().value("name").tostring();}                     catch(...){}                      if (sfb !="")                     {                         fb=new fachbereich();                         fb->name=sfb;                     }                 }                 else if (reader.name().tostring().tolower() == "stddir")                 {                     if (fb!=0) fb->stddir=reader.readelementtext().replace("\\", "/").tolower();                 }             }             else if (reader.isendelement())             {                 if (reader.name().tostring().tolower() == "fb")                 {                     if (fb!=0){                         if (!lfbs.contains(fb))                             lfbs.append(fb);                     }                 }                 else if (reader.name().tostring().tolower() == "fachbereiche") return;             }         }     } } 

so fb local variable of function. thought, creating object new memory allocated @ heap, isn't. new created, listappended reference types gone, when leaving function.

should store objects in qsharedpointer? sth. this?

qlist<qsharedpointer> lfbs; 

...

qsharedpointer<fachbereich> fb= qsharedpointer<fachbereich>(new fachbereich); 

your code doesn't have threads.

the thing want keep fb variable outside function.

and add list, good.

if (reader.name().tostring().tolower() == "fb") {     if (fb!=0){         if (!lfbs.contains(fb))             lfbs.append(fb);     } } else if (reader.name().tostring().tolower() == "fachbereiche") return; 

but in cases, , (when name "fachbereiche") directly return. don't think need put in pointer. qlist<fachbereich> saves data, , since it's member of class, data saved until class destroyed.

what do:

  • stop using pointers or new. depends on how fachbereich created, shouldn't pose problems
  • make sure element added list if needed.

for that, @ beginning of function:

fachbereich fb; bool somethingread = false; 

when deciding "create" fb, set boolean true:

if (sfb !="") {     /* maybe add debugging message here created */     somethingread = true;     fb.name=sfb; } 

replace code:

            if (reader.name().tostring().tolower() == "fb")             {                 if (fb!=0){                     if (!lfbs.contains(fb))                         lfbs.append(fb);                 }             }             else if (reader.name().tostring().tolower() == "fachbereiche") return; 

by:

break; //leave loop 

and finally, @ end of function, after loop:

/* boolean true if fb initialized */ if (somethingread) {     lfb.push_back(fb); } 

then after function called, lfb.size() of class should more 0 , lfb.last() should contain element.

qlist internally stores object after allocating them new, don't need worry that.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -