Difference between Object var and Object* var = new Object()

后端 未结 5 1010
醉酒成梦
醉酒成梦 2021-01-20 00:24

If I have a class named Object, what\'s the difference between creating an instance just like that:

Object var;

and:

Object         


        
5条回答
  •  太阳男子
    2021-01-20 01:05

    Object var();
    

    Allocates object on the stack, so you don't have to deallocate it

    Object* var = new Object();
    

    allocates object on the heap

提交回复
热议问题