When to use pointer to a class and when to just instantiate it as a variable
Posted
by
Enders
on Stack Overflow
See other posts from Stack Overflow
or by Enders
Published on 2011-01-04T22:47:38Z
Indexed on
2011/01/04
22:53 UTC
Read the original article
Hit count: 172
Im sort of confused by it. The best I could find was reading through the cplusplus.com tutorial and all they have to say about pointers to classes.
"It is perfectly valid to create pointers that point to classes. We simply have to consider that once declared, a class becomes a valid type, so we can use the class name as the type for the pointer"
Which tells me nothing about when to use them over the normal instantiation. I've seen the -> operator many times, and looked at some codes but cant really decipher why they did it.
Generic examples will be appreciated; but more specifically related to gui programming. Its where I encountered it first.
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(nameLabel, 0, 0);
mainLayout->addWidget(nameLine, 0, 1);
mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
mainLayout->addWidget(addressText, 1, 1);
Why not
QGridLayout mainLayout
mainLayout.addWidget
...
(It doesnt compile if I change the sample code to that and try it but you get the point)
Thanks in advance
© Stack Overflow or respective owner