Constructor syntax in Qt in mainwindow.cpp
-
wrote on 25 Nov 2021, 01:50 last edited by
Hi All,
I have a doubt about the syntax of the constructor implemented in the mainwindow.cpp file. Below is an example of a constructor defined in mainwindow.cpp file. I would like to be confirmed about this part of the syntax . I feel that it shows the inheritance as MainWindow is inherited from QMainWindow.
Doubts:
-
Why is the argument i.e parent same for both MainWindow and QMainWindow , for eg. MainWindow(QWidget *parent)
: QMainWindow(parent) . For MainWindow , the argument parent is of type QWidget , does this parent has same type with QMainWindow? -
What are name(0), place(0),thing(0), about(0) in the definition ?
: QMainWindow(parent),
name(0),
place(0),
thing(0),
about(0)MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), name(0), place(0), thing(0), about(0)
-
-
@JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
This is how the variables are initialized.
This is a possibility. But in proper C++ projects this is not the way members are initialised. Such code would not get approved in projects I work on unless there is really a need to do it this way.
Correct way to initialise members is using the syntax you were asking about. So, I suggest you learn and use it. This is also nothing new, exists in C++ for ages already. -
Hi All,
I have a doubt about the syntax of the constructor implemented in the mainwindow.cpp file. Below is an example of a constructor defined in mainwindow.cpp file. I would like to be confirmed about this part of the syntax . I feel that it shows the inheritance as MainWindow is inherited from QMainWindow.
Doubts:
-
Why is the argument i.e parent same for both MainWindow and QMainWindow , for eg. MainWindow(QWidget *parent)
: QMainWindow(parent) . For MainWindow , the argument parent is of type QWidget , does this parent has same type with QMainWindow? -
What are name(0), place(0),thing(0), about(0) in the definition ?
: QMainWindow(parent),
name(0),
place(0),
thing(0),
about(0)MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), name(0), place(0), thing(0), about(0)
@Swati777999 Please read https://doc.qt.io/qt-5/objecttrees.html
And also learn about C++ constructors.- Parent is of type QWidget* because any widget can be parent of other widgets. "does this parent has same type with QMainWindow?" - why don't you check documentation (https://doc.qt.io/qt-5/qmainwindow.html#QMainWindow)?
- This has nothing to do with Qt. name, place, thing and about seem to be member variables in your MainWindow class which are initialised this way in constructor. This are C++ basics, has nothing to do with Qt.
-
-
@Swati777999 Please read https://doc.qt.io/qt-5/objecttrees.html
And also learn about C++ constructors.- Parent is of type QWidget* because any widget can be parent of other widgets. "does this parent has same type with QMainWindow?" - why don't you check documentation (https://doc.qt.io/qt-5/qmainwindow.html#QMainWindow)?
- This has nothing to do with Qt. name, place, thing and about seem to be member variables in your MainWindow class which are initialised this way in constructor. This are C++ basics, has nothing to do with Qt.
wrote on 25 Nov 2021, 06:50 last edited by@jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.
:QMainWindow(parent), name(0), place(0), thing(0), about(0)
Generally, in C++ , any function is declared as
void custom_func (arg1,arg2......argn)
{
............
}Please clarify the above syntax of Qt constructor declaration.
-
@jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.
:QMainWindow(parent), name(0), place(0), thing(0), about(0)
Generally, in C++ , any function is declared as
void custom_func (arg1,arg2......argn)
{
............
}Please clarify the above syntax of Qt constructor declaration.
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
I don't understand how the member variables are represented in the declaration of the constructor with commas.
Please learn C++! You are asking absolute basics!
class SomeClass { public: SomeClass(): a(1), b("Some String") {} private: int a; std::string b; };
-
@jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.
:QMainWindow(parent), name(0), place(0), thing(0), about(0)
Generally, in C++ , any function is declared as
void custom_func (arg1,arg2......argn)
{
............
}Please clarify the above syntax of Qt constructor declaration.
wrote on 25 Nov 2021, 08:22 last edited by JonB@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
Generally, in C++ , any function is declared as
void custom_func (arg1,arg2......argn)
{This is a plain C function declaration. The example you are asking about is for a C++ class method, and inheriting from a base class.
It does not declare the variables in the comma-list, it merely initializes them. They must be declared as class members, as @jsulm has shown. In this particular case it is just equivalent to:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { name = 0; place = 0; thing = 0; about = 0; ... }
-
@jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.
:QMainWindow(parent), name(0), place(0), thing(0), about(0)
Generally, in C++ , any function is declared as
void custom_func (arg1,arg2......argn)
{
............
}Please clarify the above syntax of Qt constructor declaration.
wrote on 25 Nov 2021, 08:34 last edited by@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
I don't understand how the member variables are represented in the declaration of the constructor with commas.
As told by @jsulm : this is C++ basic knowledge: https://en.cppreference.com/w/cpp/language/constructor
As Qt is a C++ framework, it will really help you, and save from frustrations, to to first learn C++ basics.
-
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
I don't understand how the member variables are represented in the declaration of the constructor with commas.
Please learn C++! You are asking absolute basics!
class SomeClass { public: SomeClass(): a(1), b("Some String") {} private: int a; std::string b; };
wrote on 25 Nov 2021, 09:02 last edited by@jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.
I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.
-
@jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.
I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.
wrote on 25 Nov 2021, 09:12 last edited by@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
have never found the syntax of C++ function like this before
Because this is not a function but a class constructor declaration.
-
@jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.
I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
syntax of C++ function like this before
This syntax is only valid for C++ constructors, not normal functions/methods.
A C++ course should actually cover this basic thing.
Please follow the link provided by @KroMignon -
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
Generally, in C++ , any function is declared as
void custom_func (arg1,arg2......argn)
{This is a plain C function declaration. The example you are asking about is for a C++ class method, and inheriting from a base class.
It does not declare the variables in the comma-list, it merely initializes them. They must be declared as class members, as @jsulm has shown. In this particular case it is just equivalent to:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { name = 0; place = 0; thing = 0; about = 0; ... }
wrote on 25 Nov 2021, 09:29 last edited by@JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.
-
@JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
This is how the variables are initialized.
This is a possibility. But in proper C++ projects this is not the way members are initialised. Such code would not get approved in projects I work on unless there is really a need to do it this way.
Correct way to initialise members is using the syntax you were asking about. So, I suggest you learn and use it. This is also nothing new, exists in C++ for ages already. -
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
have never found the syntax of C++ function like this before
Because this is not a function but a class constructor declaration.
wrote on 25 Nov 2021, 09:42 last edited by@KroMignon Precisely, a constructor is a member function of a class.
-
@KroMignon Precisely, a constructor is a member function of a class.
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
a constructor is a member function of a class
It is a special member function of a class
-
@KroMignon Precisely, a constructor is a member function of a class.
wrote on 25 Nov 2021, 09:46 last edited by@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
Precisely, a constructor is a member function of a class.
I don't understand what your goal is?
You ask a question about very basic C++ syntax, for which you've got answer and pointer to C++ documentation to get more detailed explanation, but you don't seems to accept those responses.
Why?
Do I hurt you in any way? -
@Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:
a constructor is a member function of a class
It is a special member function of a class
@jsulm Here an explanation why this specual syntax is better:
class A { public: A() { std::cout << "A" << std::endl; } A(int) { std::cout << "A(int)" << std::endl; } }; class B { public: B() { a = A(1); } private: A a; }; B b;
In the above code you will see that both A constructors are called, even though you are creating only one A instance explicetly. Reason is that members are initialised for you if you don't do it explicetly. In this case a is initialised using default constructor first, but then you create a new one using the other constructor and assign it to a.
If you changeB() { a = A(1); }
to
B() : a(1) { }
you will see that only one constructor is called. So, it is more efficient to use the special constructor syntax.
-
@jsulm Here an explanation why this specual syntax is better:
class A { public: A() { std::cout << "A" << std::endl; } A(int) { std::cout << "A(int)" << std::endl; } }; class B { public: B() { a = A(1); } private: A a; }; B b;
In the above code you will see that both A constructors are called, even though you are creating only one A instance explicetly. Reason is that members are initialised for you if you don't do it explicetly. In this case a is initialised using default constructor first, but then you create a new one using the other constructor and assign it to a.
If you changeB() { a = A(1); }
to
B() : a(1) { }
you will see that only one constructor is called. So, it is more efficient to use the special constructor syntax.
wrote on 25 Nov 2021, 12:25 last edited by mpergandTo go along with @jsulm
With const and reference variables, using the initializer list is mandatory.class B { public: B() { a = A(1); str="hello"; // error } private: A a; const string str; };
Funny enough, that way you can initialize a const var two times !
class B { public: B() : str("hello2") { a = A(1); } private: A a; const string str="hello"; // since c++11 };
1/16