[Qt-interest] QWizardPage::wizard() overprotected?

Niels Dekker - no return address noreply at this.is.invalid
Thu Dec 11 10:00:35 CET 2008


When implementing a wizard, I got a compile error, trying to call a protected 
member function, QWizardPage::wizard(). Which should just get me a pointer 
back from any wizard page to the wizard itself.  Why isn't it public?

Anyway, I was so lucky to find a trick to get access to a protected function, 
in a standard compliant and platform independent way. In an article by 
Alexander Nasonov, Overload (ACCU), December 2005: 
http://accu.org/index.php/journals/296  Hereby I'd like to share my getWizard 
function with you:

  /// Returns the wizard associated with the specified page.
  QWizard* getWizard(const QWizardPage& page)
  {
    struct BreakProtection: QWizardPage
    {
      static QWizard* getWizard(const QWizardPage& page)
      {
        QWizard* (QWizardPage::*memberFunction)() const =
          &BreakProtection::wizard;
        return (page.*memberFunction)();
      }
    };
    // Calls the protected wizard() member function.
    return BreakProtection::getWizard(page);
  }

Please don't tell anyone  ;-)

Kind regards,

  Niels

-- 
I need to make mistakes just to learn who I am
And I don't wanna be so damn protected
  - Britney Spears, Overprotected 




More information about the Qt-interest-old mailing list