c++qtqapplicationqcoreapplication

Is qApp->exec() valid if using a QApplication


Is it valid to call qApp->exec() or QCoreApplication::exec() if I'm using a QApplication instance? Since it's a static function, in both cases QCoreApplication::exec() will be called. However, it appears that even if I call one of them, my QApplication based program works just fine - is this just luck/coincidence or is it ment to be valid?

Thanks for your help!


Solution

  • Short answer:

    It is not luck since static functions should behave like normal non-virtual functions also.

    Long answer:

    A static function is a member function that does not use this pointer. When you are calling it from an object it behaves like a normal member.

    Since QAppliction is derived from QCoreApplication and exec() is a member of QCoreApplication, it is also a member of objects of types derived from QCoreApplication.

    qApp returns a pointer to a QApplication object that is also a QCoreApplication so it also contans exec().