when should I use the new operator in C++

后端 未结 9 1975
有刺的猬
有刺的猬 2020-12-21 11:47

Say I have a class called Money which has parameters Dollars and Cents

I could initialize it in the followings 2 ways:

9条回答
  •  伪装坚强ぢ
    2020-12-21 12:25

    It is totally different.

    1. You have an object which is constructed on the stack. It will have a scope of life that lasts for a code block.

    2. You have an object initialized at some memory address allocated in the heap. It will not be destroyed until you call delete b.

提交回复
热议问题