简体   繁体   中英

class constructor initialization?

I am trying to understand the following code.(I am learning C++)

class DefaultDevice : public Device {
  public:
    DefaultDevice() :
      ui(new DefaultUI) {
    }
  private:
    RecoveryUI* ui;
};
class DefaultUI : public ScreenRecoveryUI {
 ...
}

I am having little trouble understanding ui(new DefaultUI) part. As I understand it is part of initialization before DefaultDevice() constructor is executed.
Then, from my understanding, it is going to call ReocoveryUI constructor with new DefaultUI argument. But, RecoveryUI class does not have any constructor with such argument.(sorry for not posing RecoveryUI class. it's too long :( if anyone interested in, it is Android Open source code)

so what does this 'new DefaultUI' do?

If that is a valid and working code, It seems RecoveryUI is a base class for DefaultUI .

ui(new DefaultUI) creates an object and assigns it to ui .

So, ui points to its child object.

It is not run before the ctor, it is part of the ctor. It's called an initializer list and it's used to initialize the (non-static) member variables of a class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM