You are not logged in.

#1 2021-07-05 23:48:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,867
Website

[SOLVED] c++ chaining w/ overloaded base-class constructors

I am working on code where I am deriving my own class "WebPage" from an existing class "QWebEnginePage".  The constructor for my class chains the base class constructor and also does a fair bit of work.  E.g,:

// header:
class WebPage : public QWebEnginePage {
   WebPage(QWidget *);
};
// code:
WebPage::WebPage(QWidget *parent) : QWebEnginePage(parent) {
   /* lots of code here */
}

Now I need to add an overloaded version of the constructor, but my code doesn't do anything different - I just need to pass an additional parameter to the base class.  I'd like to avoid duplicating the "lots of code" in my constructer as it would be identical between the two overloaded versions.  An approach that would work would be to separate out all that work into a different function as below, but I highly doubt this is the best approach:

// header:
class WebPage : public QWebEnginePage {
   WebPage(QWidget *);
   WebPage(QWebEngineProfile *, QWidget *);
   InitCode();
};
// code:
WebPage::WebPage(QWidget *parent) : QWebEnginePage(parent) {
   InitCode();
}
WebPage::WebPage(QWebEngineProfile *prof, QWidget *parent) : QWebEnginePage(prof, parent) {
   InitCode();
}
WebPage::InitCode() {
   /* lots of code here */
}

Is there a better approach with which I can have one function body in the code implementation but chain different constructors in the base class based on which overloaded constructor of my class was called?

Last edited by Trilby (2021-07-06 12:59:39)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#2 2021-07-06 09:54:34

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,235
Website

Re: [SOLVED] c++ chaining w/ overloaded base-class constructors

Qt seems to use default arguments for its constructors, so I guess you could go with a similar approach.

// header:
class WebPage : public QWebEnginePage {
   WebPage(QWidget *);
};
// code:
WebPage::WebPage(QWidget *parent, QWebEngineProfile *prof = QWebEngineProfile::defaultProfile()) : QWebEnginePage(prof, parent) {
   /* lots of code here */
}

This relies on QWebEngineProfile::defaultProfile() being a static method.


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3 2021-07-06 12:59:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,867
Website

Re: [SOLVED] c++ chaining w/ overloaded base-class constructors

Excellent, thanks!

Last night I went with the workaround of using the two-parameter constructor only and passing QWbEngineProfile::defaultProfile() in every instance that would have otherwise used the single-parameter version.  I don't know why a default parameter didn't occur to me.

However, this does rely on the particulars of this case that there is "defaultProfile()" value that can be used.  If there is a more general solution I'd be eager to hear it as I'm trying to better understand C++, which I must admit much ignorance in at the moment.  In anycase, this certainly solves the present issue.

As a little revision, I'll have the default parameter defined in the class definition / header file, otherwise I'm not sure if code in other modules could use the two-parameter version as such a function would not be defined in their scope.

Last edited by Trilby (2021-07-06 13:00:59)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2021-07-06 15:11:48

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,267
Website

Re: [SOLVED] c++ chaining w/ overloaded base-class constructors

Beware that default arguments can be nasty.
I'd suggest to rather overload the constructor:

// code:
WebPage::WebPage(QWidget *parent, QWebEngineProfile *prof)
  : QWebEnginePage(prof, parent)
{
   /* lots of code here */
}

// code:
WebPage::WebPage(QWidget *parent)
  : WebPage(parent, QWebEngineProfile::defaultProfile())
{
}

PS: I don't know what the WebPage class is intended for in detail, but consider the Liskov Substitution from the SOLID principles. I.e. do you really want inheritance or would encapsulation be a better fit than inheritance?

Last edited by schard (2021-07-06 15:22:07)

Offline

#5 2021-07-06 15:31:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,867
Website

Re: [SOLVED] c++ chaining w/ overloaded base-class constructors

I didn't realize a different overloaded constructor of the same class could be chained - that works well also for these purposes (where there is a good default that can be passed).

This must be a subclass so that the derived class can be set as the page for a webview.  In qt5webengine there are qwebengine view and qwebpage classes, the former always contains one of the latter.

Last edited by Trilby (2021-07-06 15:34:48)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB