PDA

View Full Version : what does the constructor do here?



LB4229
21st June 2011, 16:37
is this a type of initialization? the constructor ButtonWidget(int widgets) seems doesnt initialize int widget. could anyone explain it in detail=? thank you.
I donot understand the code in buttonwidget.cpp



//buttonwidget.cpp
ButtonWidget::ButtonWidget( int widgets )
: QWidget()
{
QVBoxLayout *vbox = new QVBoxLayout();

if ( widgets & ButtonWidget::ComboBoxes )
vbox->addWidget( comboBoxes() );

if ( widgets & ButtonWidget::SpinBoxes )
vbox->addWidget( spinBoxes( widgets & ButtonWidget::SpinBoxexUpdateButton ) );

if ( widgets & ButtonWidget::CheckBoxes )
vbox->addWidget( checkBoxes() );

if ( widgets & ButtonWidget::ParetoBoxes )
vbox->addWidget( paretoBoxes() );

if ( widgets & ButtonWidget::MarknDiffBoxes )
vbox->addWidget( markndiffBoxes() );

vbox->addStretch();

setLayout(vbox);
}




//buttonwidget.h
........
class ButtonWidget : public QWidget
{
Q_OBJECT

public:
enum Widgets
{
ComboBoxes = 1,
SpinBoxes = 2,
SpinBoxexUpdateButton = 4,
CheckBoxes = 8,
ParetoBoxes = 16,
MarknDiffBoxes = 32
};

ButtonWidget( int widgets );
~ButtonWidget();
.........
private:
QGroupBox* comboBoxes();
QGroupBox* spinBoxes( bool updateButton );
QGroupBox* checkBoxes();
QGroupBox* paretoBoxes();
QGroupBox* markndiffBoxes();


I just know the initialization as below. What kind of initialization is it in button.cpp?


class Something
02 {
03 private:
04 int m_nValue;
05 double m_dValue;
06 int *m_pnValue;
07
08 public:
09 Something()
10 {
11 m_nValue = 0;
12 m_dValue = 0.0;
13 m_pnValue = 0;
14 }
15 };

squidge
21st June 2011, 17:00
Its a constructor initialisation list, as described here: http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/

LB4229
21st June 2011, 17:12
thank you for your reply. I have read that already. But here it seems has nothing to do with initialization. This if -condition makes me confused. Because usually we use only initialization in constructor. But why here use "if" in constructor? could someone give me another simpler example? Maybe it is helpful for me to understand it.

squidge
21st June 2011, 18:15
You can use whatever code you like in the constructor, in this case various widgets are added depending on the value to pass to the constructor.

For example, if you pass ButtonWidget::ComboBoxes, then you get ComboBoxes, if you pass ( ButtonWidget::ComboBoxes | ButtonWidget::SpinBoxes) then you'll get boith ComboBoxes and SpinBoxes, etc.

If you really wanted to you could write an entire application in your constructor, but its not best practice to do so.

LB4229
21st June 2011, 19:09
:D thank you. I thought the constructor would be used only for initialization.
Your explanation enlightens me.

ChrisW67
21st June 2011, 22:32
The constructor is only used for initialisation in the ButtonWidget. The 'widget' parameter is used to drive how the ButtonWidget is initialised using other methods (comboBoxes() etc.) in the class.

Santosh Reddy
21st June 2011, 23:52
Added to what squidge said, constructor is basically a special function which is called (automatically, compiler generates the required code) when object is created, you can do all the things in it which you could do in normal function, added to normal function constructors can take initialization list. Also there types of constructors like default constructor, copy constructor etc. which are beyond the scope of this post.

DanH
22nd June 2011, 03:19
The parameter "int widgets" isn't really an int (number), it's a bit set. The user of the class would specify a parameter that is the bitwise OR of some subset of the "enum Widgets" values -- eg "ComboBoxes | CheckBoxes". This then tells the constructor which widgets to add to the layout.