The EOP for schoolchildren. - page 14

 
Alexey Viktorov:

One more question: What is the difference between declaring a variable, getting/creating a pointer, creating an object instance. It's not the difference, but in which cases it's better to use. For example, it's enough to declare a variable for opening a position.

And in what cases it's better to use the pointer, and in what cases we can't do without the object instance.

What are the pros and cons of one method or another?

So, get into the guts of this class, it's available in the cheese. Nothing important is created there, just a set of methods.

 
Alexey Viktorov:

So it was to understand what to choose that the question was asked. Not for a contest of witch doctors.

I'm not sober now, even very sober, but I'll give it a try. If reliability is your first vein, then following the RAII principle is your everything, if possible (you declare an object, the compiler will kill it when it leaves the visibility area). If you want code efficiency, everything is looking in the direction of pointers. So, it's up to each task to think it through, there is no universal solution.
 
Alexey Viktorov:

So it was to understand what to choose that the question was asked. Not for a contest of witch doctors.

The way I understand it.
If an object is created per stack, then only for those objects that don't live long and need the speed to create an object, for example a class for working with requests.
Objects on the heap should be created for long-lived objects that don't require super speed, or that are important to avoid losing them.
Sometimes, for example in VS, the compiler won't let you choose a stack and only offers a heap, i.e. tells you what is best for this object.

 
Roman:

The way I understand it.
If an object is created per stack, then only for those objects that do not live long and need the speed of object creation, for example, a class for working with requests.
Objects on the heap should be created for long-lived objects that don't require super speed, or which are important to avoid losing them.
And sometimes, for example in VS, the compiler does not allow to choose a stack and offers only a heap, i.e. tells you what is better for this object.

No. I confess, I was mistaken too some time ago. When an object is created on the stack, it is allocated in the heap anyway, only the pointer is created on the stack. Simply, when declaring CObg obj, its destructor is guaranteed to be called when obj leaves the scope.
 
Vladimir Simakov:
No, I don't. I'm sorry, I was mistaken once too. When an object is created on the stack, it is allocated in the heap anyway, only the pointer is created on the stack. It's just that when declaring CObg obj, its destructor is guaranteed to be called when obj leaves its visibility area.

Maybe it will be allocated on the heap, I don't know ))
But many people claim that the object is created faster on the stack.
Apparently everyone is mistaken about it, I don't know ))

 
Roman:

Maybe it does allocate to the pile, I don't know ))
But a lot of people claim that on the stack the object is created faster.
Apparently everyone is mistaken about it, I don't know ))

What's stopping you, when in doubt, from checking it out? Why continue to poke fun at it if it's already been tested and proven?

 
Dmitry Fedoseev:

What's stopping you, when in doubt, from checking it out? Why continue to fuck around when it has already been tested and proven?

I have no doubts )) But also to check, somehow it hasn't come to that.
You just wrote about it earlier, so if it has already been tested and proven, we can trust you as an old-timer coding )).
I don't really care about this difference, stack, pile, but sometimes I do.

 

Guys, I'm a retrograde, but there is a stack (when the first stored value is retrieved last) and a queue (when first in, first out).

What is a heap?

 
Alexey Viktorov:

So it's for understanding what to choose that was the question asked. Not for a quiz of the wizards.

Yes, I forgot to add that if you create several objects on a heap, it is recommended to delete them in reverse order of their creation.

 
Roman:

Yes, I forgot to add that if multiple objects are created on the heap, it is recommended to remove them in reverse order of their creation.

So the heap is a stack.