Constructor syntax in Qt in mainwindow.cpp
-
@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'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.
@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; ... }
@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.
@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.
@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.
To 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 };