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

后端 未结 5 1007
醉酒成梦
醉酒成梦 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 00:53

    Object var();
    

    Declaration of a function that returns Object. To create an automatic object(i.e on the stack):

    Object var; // You shouldn't write the parenthesis.
    

    While:

    Object* var = new Object();
    

    Is a dynamically allocated Object.

提交回复
热议问题