volperossa
volperossa

Reputation: 1431

C++ list: copying a local list in a member list

in a method, I have a local variabile

std::list<proiezione> ora;    

and a member variable of same type

std::list<proiezione> orb;   

In my method I have

for (std::list<proiezione>::iterator it = ora.begin(); it != ora.end(); ++it)
    this->orb.push_back(*it);

but doesn't work!

this->mem is empty! why?

precisely:

class CMFCApplication4Doc : public CDocument
{
public:
std::map<CString, Esame> esami;
INT valore_lode;
proiezione pr;
std::list<proiezione> orb;
    void get_proiezione(FLOAT media_desiderata);
}

void CMFCApplication4Doc::get_proiezione(FLOAT   media_desiderata)
{
    std::list<proiezione> ora;
std::vector<CString> v_proiezione;
CString appoggio;
std::map<CString, Esame> es = esami;
calcola_proiezione(ora,&pr, es, media_desiderata,valore_lode);
for (std::list<proiezione>::iterator it = ora.begin(); it != ora.end(); ++it)
    this->orb.push_back(*it);
 ecc ecc (I don't touch orb anymore)

} 

in debug mode I have "ora" with 25 elements, but "this->orb" with zero elements !

Upvotes: 0

Views: 228

Answers (1)

Zac Howland
Zac Howland

Reputation: 15870

Why not use the std::copy algorithm?

std::copy(tList.begin(), tList.end(), std::back_inserter(mem));

Or use copy-swap:

void MyClass::func(std::list<proiezione> tList)
{
    mem.swap(tList);
}

Or use the assign member:

mem.assign(tList.begin(), tList.end());

Or use the copy-assignment operator:

mem = tList;

Without seeing more code, it would be hard to tell you why it is empty.

Almost forgot (credit Casey with reminding me): or is actually a reserved word, so you'll want to name your variable something else.

Upvotes: 1

Related Questions