PDA

View Full Version : best object creation method



nuliknol
12th June 2014, 02:16
Hi,
I would like to ask , which declaration of object is faster?



QLabel lbl_name;
lbl_name.setText("Name:");

or this:


QLabel *lbl_name;
lbl_name= new QLabel("Name:");


Or in other words, is it best to use pointers to objects or objects themselves ?
I am going to use this for creating dialogs and windows in my app.
TIA

wysota
12th June 2014, 07:41
When creating instances of subclasses of QObject it is almost always required to use pointers.

anda_skoa
12th June 2014, 09:53
The fastest option is most likely a combination


QLabel label("text");

i.e. on the stack and passing the text to the constructor instead of calling a setter.

However, as wysota has already said, widgets are most often created on the heap because they usually form a tree and their respective parent widget will attempt to delete them on its own destruction.
Which would not go together well with an object created on the stack

Cheers,
_