phoenix::Tutorial - References

phoenix is based around the idea of using actual objects, and passing references to them when needed. This is in contrast to the design of eg Qt and GTK+, which prefer pointers.

The reason I prefer references is because there is less red-tape, and no ambiguity when it comes to null pointers. Null pointers are simply not permitted.

Because we use references, this means there is no need for constructor parameters or calls to new. It also means that with phoenix, deleting objects is something that is meant to happen at application close.

Take the following example:

#include <phoenix/phoenix.hpp>
using namespace nall;
using namespace phoenix;

struct TestWindow : Window {
  VerticalLayout layout;
  RadioBox radio1;
  RadioBox radio2;

  void create() {
    setGeometry({ 128, 128, 640, 480 });
    radio1.setText("Radio Button 1");
    radio2.setText("Radio Button 2");
    RadioBox::group(radio1, radio2);
    layout.append(radio1, {0, 0}, 5);
    layout.append(radio2, {0, 0});
    append(layout);
    onClose = &OS::quit();
    setVisible();
  }
} testWindow;

int main() {
  testWindow.create();
  OS::main();
  return 0;
}

In this example, the only construction needed was for attaching the actual widgets to the window. We are free to work with the RadioBox widgets even before they are actually attached to the window.

No effort is necessary for creating nor for freeing any resources.

Now, of course, if you really wanted to, you could create windows and widgets at run-time by using operator new, you would just need to cast your pointers to references by prefixing them with *. I do not recommend this, however, and phoenix expects to destroy widgets on its own at application termination. Attempting to destroy resources that phoenix may still expect to be valid can lead to very bad things.