PDA

View Full Version : initializing object



nuliknol
7th February 2015, 22:32
Hi! Simple question here...
Can and object be initialized inside "private:" clause ? Something like this:



class WAction : public QWidget
{
Q_OBJECT
public:
explicit WAction(QWidget *parent = 0);

signals:
public slots:
private:
QPushButton btn_new_action("New Action");
QLabel lbl_description("Action description:");
};


if so, what would be the syntax, because the above code is not valid.

anda_skoa
7th February 2015, 22:49
Yes, using C++ initializer list feature



WAction::WAction(QWidget *parent)
: QWidget(parent), btn_new_action("New Action"), lbl_description("Action Description")
{
}


Cheers,
_

ChrisW67
8th February 2015, 21:22
Fairly unusual to have actual instances of QWidgets as member variables... Usually would be QWidget*.