14

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

Guillaume
  • 1,022
  • 1
  • 9
  • 17
  • Possible duplicates: http://stackoverflow.com/questions/4029970/c-what-are-scenarios-where-using-pointers-is-a-good-ideatm-closed http://stackoverflow.com/questions/2144698/common-uses-for-pointers – Benjamin Lindley Jan 04 '11 at 22:52
  • 1
    Make sure you don't make the mistake of thinking "Qt does things this way, so C++ does things this way." I think many people, me included, dislike the way Qt does many things, and memory management is one of them. But when in Rome... – GManNickG Jan 04 '11 at 22:56
  • Pointers do not point to classes. Pointers point to objects. – Lightness Races in Orbit Jan 04 '11 at 23:41
  • 3
    "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" isn't terribly accurate writing. cplusplus.com is known to be riddled with errors and is not generally considered a good resource. Internet tutorials are, in general, not considered a good resource for learning C++. Pick a good book from http://jcatki.no-ip.org/fncpp/Resources instead. :) – Lightness Races in Orbit Jan 04 '11 at 23:49

5 Answers5

11

A good way to think about when to stack-allocate (non-pointer) versus heap-allocate (pointer) an object is to think about how long you want that object to live. If you put the object on the stack as a local variable, then it will be cleaned up and cease to exist as soon as the function returns. If you want the object to outlive the function call that created it, put it on the heap.

With the example of the grid layout, I believe that the pointer version is more appropriate because after the function call that creates the layout returns, you still want to have the layout lying around. Otherwise, the layout would exist only as long as the function was running.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Heap and stack are outmoded, confusing and inaccurate terms for storage duration. Objects with automatic storage duration are automatically destroyed when the block in which they are created exits. Objects with dynamic storage duration are created with a new-expression, and their lifetime lasts until they are destroyed using a delete-expression. You may obtain a pointer to either type of object. – Lightness Races in Orbit Jan 04 '11 at 23:43
2

One good reason is polymorphism and dynamic dispatching. By having a pointer declared to a base class type and assigning it to an instance of a subtype, you could be invoking different versions of the method. In the above examples, there is probably some base type (maybe QTLayout?) with which the pointer can be declared.

Another reason is a situation where you want your object to "live" beyond the scope of the method in which it is declared. Your second example is allocated on the stack and will be deallocated when you leave.

Uri
  • 88,451
  • 51
  • 221
  • 321
1

The real question is not about the use of pointers, but about dynamic allocation. Dynamically allocating an object (with new) allows you to control the lifetime of the object. If you want to create the object in a function but don't want it to cease to exist as soon as the function is finished, then dynamic allocation is for you.

You'll usually see pointers pointing to dynamically-allocated objects (as this is the best way to deal with them, references being a clumsy alternative); however, you can have a pointer to an object with automatic storage duration too:

QGridLayout mainLayout;
QGridLayout* ptr = &mainLayout;
ptr->addWidget(nameLabel, 0, 0);
ptr->addWidget(nameLine, 0, 1);
ptr->addWidget(addressLabel, 1, 0, Qt::AlignTop);
ptr->addWidget(addressText, 1, 1);

^ In this example, I'm using automatic storage (by creating the object as a "normal variable" as you put it), so the object will be destroyed at the end of the enclosing block scope (probably the end of the function, or perhaps of a conditional (if) block), but still using a pointer with it!

Hope that helps somewhat.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Is QGridLayout a class? The second one should work just fine if you fixed the typos. You said it doesn't compile. Should we guess at the error?

Creating the object as a local instance is just fine. The only potential problem is if the class is large, as this tends to use up the stack space excessively.

Note that an object can allocate all the memory it needs to and that won't impact its size on the stack.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

Employing a ptr to a class versus instantiation on the stack also allows for conditional existence of the object, ie the value NULL, if you need that as a handled case.

no-op
  • 131
  • 4