c++ - How to declare a function which creates an object which will never be null? -
i have function creates object qmenu (in heap)
qmenu* createmenu(qwidget* parent); // parent takes ownership of menu
the function never return null pointer. think declaration don't tell point because returns pointer. when using method, need
if (qmenu* m = createmenu(parent)) m->...
which annoying. if returns reference, tells point.
qmenu& createa(qwidget* parent);
i never see code declares way. okay? there better declaration point?
yes, it's fine return created object reference, if function makes own arrangements ownership of object's lifetime.
one big example of function this: v& std::map<k,v>::operator[](const k&);
.
Comments
Post a Comment