c++ - When should I use pointer or reference members, and when should I pass nullptr or this as parent pointer in Qt? -


the code

i've been writing c++ qt knowing works not understanding why things other "i know should doing this".

this startup class initialises classes:

namespace gui {     startup::startup() :         qobject(nullptr),         m_setuptab(*new setuptab(nullptr)),         m_regtab(*new cbcregisterstab(nullptr)),         m_datatab(*new datatesttab(nullptr)),          m_mainview(*new mainview(nullptr,                                  m_setuptab,                                  m_regtab,                                  m_datatab)),          m_systemcontroller(*new systemcontroller(nullptr,                                                  provider::getsettingsassingleton())),          m_datatest(*new datatest(nullptr,                                 m_systemcontroller)),          m_setuptabvm(new setuptabviewmanager(this,                                              m_setuptab,                                              m_systemcontroller,                                              provider::getsettingsassingleton() ))      {      } 

then in header file member variables described such:

private:     setuptab& m_setuptab;     cbcregisterstab& m_regtab;     datatesttab& m_datatab;      mainview& m_mainview;     settings* m_settings;      systemcontroller& m_systemcontroller;      datatest& m_datatest;      setuptabviewmanager* m_setuptabvm; 

the main difference between view manager , else view manager passes signals between tab classes , else.

then start in main, this:

    gui::startup startup;     startup.show();   setuptabviewmanager.cpp:  #include "setuptabviewmanager.h" #include "view/setuptab.h" #include "model/systemcontroller.h" #include "model/settings.h" #include <qdebug>  namespace gui {     setuptabviewmanager::setuptabviewmanager(qobject *parent,                                              setuptab& tab,                                              systemcontroller& sysctrl,                                              settings& config) :         qobject(parent),         m_setuptab(tab),         m_systemcontroller(sysctrl)     {         wiremessages(config);         wiresetuptabbuttons(config);     }      void setuptabviewmanager::wiremessages(settings& config)     {         connect(&config, signal(notifystatusmessage(qstring)), //for qt4                 &m_setuptab, slot(onstatusupdate(qstring)) );          connect(&m_systemcontroller, signal(notifystatusmessage(qstring)),                 &m_setuptab, slot(onstatusupdate(qstring)));     }      void setuptabviewmanager::wiresetuptabbuttons(settings& config)     {         connect(&m_setuptab, signal(onbtnloadsettingsclicked(bool)),                 &config, slot(onloadbuttonclicked(bool)) );          connect(&config, signal(sethwtree(qstandarditemmodel*)),                 &m_setuptab, slot(sethwtreeview(qstandarditemmodel*)));          connect(&m_setuptab, signal(onbtninitclicked()),                 &m_systemcontroller, slot(startinitialisehw()));          connect(&m_setuptab, signal(onbtncfgclicked()),                 &m_systemcontroller, slot(startconfigurehw()));      } } 

questions

what advantage of initialising class in member variable pointer or reference? know when make "view manager", should initialise member variable pointer. though i'm not sure why?

also advantage of "this" or "nullptr" parent class?

qt objects organized in object trees. allows programmer not care desctruction of created objects: deleted automatically when respective parents deleted.

if take @ qt gui application, see of widgets (buttons, checkboxes, panels etc.) have 1 single ancestor - main window. when delete main window, application widgets automatically deleted.

you can set parent-child relation passing parent object newly created child:

qwidget *mainwidget = ... qpushbutton *btn = new qpushbutton(mainwidget); 

in case btn object becomes child of mainwidget object, , moment may not delete child manually.

if create object without parent, should delete yourself.

as thuga mentioned in comments, can transfer object ownership later. widgets can places inside layouts , make them children of layout owner. or can explicitly set parent qobject::setparent.

and declaring class memebers pointers or references - not matter. it's question of convenience.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -