PDA

View Full Version : How get a reference to a QList



franco.amato
20th January 2011, 15:30
Hi to all,
in my program I need to use a QList<MyStructure *> list, so I declared one in the private section of my class. I also need a method to get it ( a reference to it ).
This is some code:


struct ScanRec;

class VistaIrisMatching : public QDialog
{
Q_OBJECT

public:
VistaIrisMatching(QWidget *parent = 0, Qt::WFlags flags = 0);
~VistaIrisMatching();
Ui::VistaIrisMatchingClass ui;
m_scanRecList& getScanRecList(); // <------ get a reference to the list

public slots:
void onUpdateDisplay( IplImage* );
bool onLoadDBRecords();

private:
CVistaCamera* m_vistaCamera;
QSqlDatabase IrisDB;
QTimer* m_timer;
QList<ScanRec *> m_scanRecList; // <------- the list
};

I don't understand why it doesn't compile and the compiler says that the error is in the
m_scanRecList& getScanRecList();

Where am I wrong?
Best Regards

high_flyer
20th January 2011, 15:51
This:

m_scanRecList& getScanRecList();
Makes no syntactical nor logical sense.
When you declare a method, you declare a return TYPE to it, and you don't use a variable for that.


QList<ScanRec *>& getScanRecList(); // <------ get a reference to the list

franco.amato
20th January 2011, 16:00
But then I would be sure that the returned reference it to my list

Added after 5 minutes:

Sorry I noticed now that I did a very big mistake mmmm my poor c++.
Thanx again

Lykurg
20th January 2011, 16:40
If you return a non const reference, you could make the list public and skip the getter method.

SixDegrees
20th January 2011, 21:52
If you return a non const reference, you could make the list public and skip the getter method.

Quite correct. It makes no sense to return a reference to an internal private object through an accessor function, since you're effectively granting public access to the object by doing so.