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,350
  • 9
  • 47
  • 87

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
  • 27,051
  • 3
  • 32
  • 62
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
  • 714,442
  • 84
  • 1,110
  • 1,523