What is the difference between
Class class("Test");
and
Class *class = new Class("Test");
Which one is better? Is there even a difference?
What is the difference between
Class class("Test");
and
Class *class = new Class("Test");
Which one is better? Is there even a difference?
Class class("Test");
This object is on stack. It will be destroyed after the function it is in returns. Normally, stack allcation is faster than heap. But its size is limited (a few MBs, depends on compiler).
Class *class = new Class("Test");
This object is on heap. It should be deleteed by programmer after finish using. heap is useful for large objects (for example, if your variable is larger than the whole stack, it can only be allcated on heap), and memory reservation. Its size can be considered as the entire memory space.