Best Practice: QT4 QList<Mything*>... on Heap, or QList<Mything> using reference?

Posted by Mike Crowe on Stack Overflow See other posts from Stack Overflow or by Mike Crowe
Published on 2010-03-28T14:07:30Z Indexed on 2010/03/28 14:13 UTC
Read the original article Hit count: 421

Filed under:
|

Hi Folks,

Learning C++, so be gentle :)...

I have been designing my application primarily using heap variables (coming from C), so I've designed structures like this:

QList<Criteria*> _Criteria;
// ...
Criteria *c = new Criteria(....);
_Criteria.append(c);

All through my program, I'm passing pointers to specific Criteria, or often the list. So, I have a function declared like this:

QList<Criteria*> Decision::addCriteria(int row,QString cname,QString ctype);
Criteria * Decision::getCriteria(int row,int col)

which inserts a Criteria into a list, and returns the list so my GUI can display it.

I'm wondering if I should have used references, somehow. Since I'm always wanting that exact Criteria back, should I have done:

QList<Criteria> _Criteria;
// ....
Criteria c(....);
_Criteria.append(c);

...

QList<Criteria>& Decision::addCriteria(int row,QString cname,QString ctype);
Criteria& Decision::getCriteria(int row,int col)

(not sure if the latter line is syntactically correct yet, but you get the drift).

All these items are specific, quasi-global items that are the core of my program.

So, the question is this: I can certainly allocate/free all my memory w/o an issue in the method I'm using now, but is there are more C++ way? Would references have been a better choice (it's not too late to change on my side).

TIA

Mike

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt4