When to use a pointer to a class and when to just create it as a variable

I was a little confused. The best I could find was to read the cplusplus.com tutorial and all they have to say about class pointers.

"The creation of pointers pointing to classes is absolutely true. We just have to consider that after the declaration, the class becomes a valid type, so we can use the class name as the type for the pointer"

This does not tell me anything about when to use them in a regular instance. I repeatedly saw the operator → and looked at some codes, but could not understand why they did it.

Common examples will be appreciated; but more specifically related to guy programming. This is where I came across this in the first place.

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 does not compile if I change the sample code to this and try, but you get the point)

Thanks in advance

+8
c ++ pointers
source share
5 answers

A good way to think about when to allocate a stack (not a pointer) versus a heap-allocation (a pointer), an object should think about how long you want this object to live. If you push an object onto the stack as a local variable, it will be cleared and cease to exist as soon as the function returns. If you want the object to stop calling the function that created it, put it in a heap.

Using the example of a grid layout, I believe that the version of the pointer is more appropriate, because after calling the function that creates the layout, you still want the layout to lie. Otherwise, the layout will exist only as long as the function is launched.

+9
source share

One good reason is polymorphism and dynamic scheduling. By pointing to the type of the base class and assigning it to an instance of the subtype, you can call different versions of the method. In the above examples, there is probably some base type (perhaps QTLayout?) With which a pointer can be declared.

Another reason is the situation when you want your object to "live" outside the scope of the method in which it is declared. The second example is allocated on the stack and will be freed when you leave.

+2
source share

The real question is not the use of pointers, but the dynamic allocation. Dynamic allocation of an object (using new ) allows you to control the lifetime of an object. If you want to create an object in a function, but do not want it to cease to exist once the function has been completed, dynamic allocation is for you.

You usually see pointers pointing to objects with dynamic allocation (since this is the best way to handle them, links are a clumsy alternative); however, you can also specify a pointer to an object with automatic storage duration:

 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 use automatic storage (creating the object as a "normal variable", as you put it), so the object will be destroyed at the end of the enclosing block area (perhaps the end of the function or, possibly, the conditional ( if ) block), but still using the pointer with her!

Hope this helps.

+1
source share

Is QGridLayout a class? The second should work fine if you correct typos. You said that it does not compile. Should we assume a mistake?

Creating an object as a local instance is just fine. The only potential problem is that the class is large, as it tends to overuse the stack space.

Note that an object can allocate all the memory it needs, and this will not affect its size on the stack.

0
source share

Using ptr for a class in comparison with an instance on the stack also allows the conditional existence of the object, that is, a NULL value if you need it as a processed case.

0
source share

Source: https://habr.com/ru/post/650692/


All Articles