PDA

View Full Version : Someone please help me understand the syntax.



rookee
23rd October 2015, 19:13
Someone please help me understand the syntax and what is happening in these below lines of the code from the link http://ldc.usb.ve/docs/qt/tutorial-t5.html. Thanks in advance.

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0); <== is MyWidget a class a function or a constructor?
};

MyWidget::MyWidget(QWidget *parent): QWidget(parent) <== What is this syntax and what does it mean?

ravas
23rd October 2015, 19:59
MyWidget(QWidget *parent = 0); <== is MyWidget a class a function or a constructor?
That is the declaration of the constructor


MyWidget::MyWidget(QWidget *parent): QWidget(parent) <== What is this syntax and what does it mean?
Past the colon is the initialization list. You are passing the parent to the base class with QWidget(parent)

Radek
23rd October 2015, 20:17
Aye, aye ... I don't want to be rude but you should know that Qt is a set of C++ libraries, rookee. The meaning of the "things" above is a very basic C++. The first "thing" is a declaration of a constructor in a header, the second one is a code of the constructor specifying a constructor of the base class which should be called and how. Please, look at some C++ book, you cannot pass without a basic knowledge of C++. In fact, you will need considerably more than a basic knowledge.

Hossein
24th October 2015, 18:53
Someone please help me understand the syntax and what is happening in these below lines of the code from the link http://ldc.usb.ve/docs/qt/tutorial-t5.html. Thanks in advance.

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0); <== is MyWidget a class a function or a constructor?
};

MyWidget::MyWidget(QWidget *parent): QWidget(parent) <== What is this syntax and what does it mean?

If you review the chapters explaining classes in c++, you will know everything here.
basically

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);

};
you have a class named MyWidget, which inherits from QWidget. so when you do create your classes, most of the time you also define a constructor for it.
and the constructor has the name of your class, without any return type, It can take any number of parameters, including none!
but since you are inheriting from something else, in order to get things working, if the base class needs something , you need to give what it needs
and here we get it from the user and passes it to the base class's constructor.
thats why we have a QWidgets* parameter for our constructor,
Now we could also have the definition written here as well, which would look like this :

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0) :QWidget(parent)
{
....
}

};
This is kind of equivalent to

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0)
{
QWidget(parent)
}
};

And what is trying to say is that, first build the base part, gives it what ever it needs to properly initialize, and then continue with my specific customization.
(and by the way, the second snippet which I gave you is not valid, I just wrote it as a hint of what happens here , you need to use the first style for initialing the base constructors )

and finally, it is good programming practice to separate the class signature definition and their implementation .
so to do that we can also do it like this :
this is class definition (signature if you will )

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);

};

and this is the implementation details

MyWidget::MyWidget(QWidget *parent): QWidget(parent)
{}
the first part says, the remaining belongs to the MyWidget class

you can see two simple example of both cases here :
http://ideone.com/4XXZJw
http://ideone.com/B4CtyX

Hope this helps

rookee
28th October 2015, 18:04
@Radek: I've never done programming before, sorry for my dumb questions.

Thank you all for your valuable time. So kind of you all.

ravas
28th October 2015, 21:10
A random assortment from my bookmarks:

https://isocpp.org/faq
http://www.learncpp.com/
http://en.cppreference.com/w/cpp
http://stackoverflow.com/tags/c%2b%2b/info
http://www.cprogramming.com/tutorial/c++-tutorial.html