c ++:新的Object()和Object()之间的区别是什么? [英] c++: what's the difference between new Object() and Object()
问题描述
所以在C ++中你可以使用new关键字或其他实例化对象...
so in C++ you can instantiate objects using the new keyword or otherwise...
Object o = new Object();
但您也可以执行
Object o = Object();
两者之间的区别是什么,为什么要使用一个呢?
what exactly is the difference b/w the two and why would I use one over the other?
推荐答案
你不能做 Object o = new Object();
new
运算符返回一个指向类型的指针。它必须是 Object * o = new Object();
Object
堆。
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()
将创建对象
堆栈上的实例。我的C ++是生锈的,但我相信,虽然这天真地看起来像一个创作,然后一个作业,它实际上将作为一个构造函数调用。
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.
这篇关于c ++:新的Object()和Object()之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!