Call a member function right after the constructor

The general question:
How can i call a member function right after the constructor?

Why did i hit that question:
I wanted to create a class with a complete ofApp(with a window) in it.
Because of the ofxQt addon I was trying to create a qt widget (let’s call it QofApp which inherits ofApp) that had an ofWindow in it. One would subclass from QofApp write their Of code and that would run in Qt. So far it works quite well, but there are some ugly things i would like to polish.

As it is now, one has to create an instance of the QofApp subclass and right after that call the startRender function (see the example) which calls ofRunApp. the reason for that is that it seems i can not call ofRunApp in the QofApp class constructor because obviously the ofApp is not constructed yet.

Any ideas on this weird issue?

you can’t :slight_smile:

you could create an static function instead that creates the instance, calls the method on it and returns the instance.

the architecture is a bit weird though, i guess it’s because of how qt works but why not just create an ofAppQtWindow that wraps the window creation using qt and then just use the traditional OF way of starting the app in main by passing an ofQtWindowSettings? like:

int main(){
    ofQtWindowSettings settings;
    auto window = ofCreateWindow(settings);
    auto app = make_shared<ofApp>();
    ofRunApp(window, app);

you can even change ofRunMainLoop with something like ofRunQtMainLoop in case you need to do that to run inside qt’s own main loop.

1 Like

Thank you very much again Arturo,

Most probably the architecture is weird because i arrived to it by trial and error, and also trying to cover as many cases as i found. It would be great if somebody else could have a look at it.

The main reason why i don’t use the the strategy you posed is because i want to allow people to use the addon in other ways. For example inserting ofWindows in QtWidgets, as many as they want. In that case the addon is calling OF to render only when Qt asks for it, so OF is a slave of Qt, and the OF loop is bypassed but the events must keep working too.

It is true that i should have a look at the ofWindowSettings features… haven’t used that too much yet.