c++: what's the difference between new Object() and Object()

后端 未结 3 1482
孤城傲影
孤城傲影 2021-01-07 08:17

so in C++ you can instantiate objects using the new keyword or otherwise...

Object o = new Object();

but you can also just do



        
3条回答
  •  走了就别回头了
    2021-01-07 08:41

    You can't do Object o = new Object(); The new operator returns a pointer to the type. It would have to be Object* o = new Object(); The Object instance will be on the heap.

    Object o = Object() will create an Object instance on the stack. My C++ is rusty, but I believe even though this naively looks like an creation followed by an assignment, it will actually be done as just a constructor call.

提交回复
热议问题