V
V
Vitaly2016-09-21 21:11:49
OOP
Vitaly, 2016-09-21 21:11:49

How do you create objects differently?

What is the difference between creating an object in this way Object obj;and thisObject *obj = new Object;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2016-09-21
@petermzg

Object obj;on the stack and the destructor will be called automatically when you exit the scope
Object *obj = new Object;on the "heap" and you will have to delete the object yourself

D
Denis Zagaevsky, 2016-09-21
@zagayevskiy

1. Memory. In the first case, the memory for the object will be allocated on the stack, in the second - on the heap.
2. Life time. In the first case, the object exists until the scope is exited, in the second case, until delete obj is called;

A
Andrey Inishev, 2016-09-21
@inish777

The first is created on the stack, the second is allocated memory in the heap.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question