0
Note::Note(Traymenu *trayMenuIn, QWidget *parent) :
    ui(new Ui::Note){
    ui->setupUi(this);

Note::Note(Traymenu *trayMenuIn, QWidget *parent){
    ui = new Ui::Note;
    ui->setupUi(this);

Both kinds are working. The above code is suggested by QtCreator, the lower code is what I would do if I had to write it on my own.

Note's private member is

Ui::Note *ui;
user2366975
  • 4,018
  • 7
  • 42
  • 83

2 Answers2

4

Only the first form is initialization. The second form initializes ui with an undefined value, then assigns a value to it.

You should prefer the first form (initialization). See the related C++ FAQ entry.

Christian Hackl
  • 26,144
  • 3
  • 29
  • 57
2

Although the difference is tiny, I would prefer the code snippet suggested by QtCreator: it uses the initialization syntax rather than the assignment syntax for the code that logically represents initialization.

Since the member being initialized is a pointer, there is no performance penalty even with the optimization turned off. However, it is a good idea to get into a habit of initializing as much as possible with the initialization list, because this prevents potential coding issues inside the constructor itself.

Sergey Kalinichenko
  • 697,062
  • 78
  • 1,055
  • 1,465