Object var和Object * var = new Object()之间的区别

时间:2022-03-13 22:28:56

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

如果我有一个名为Object的类,那么创建一个实例之间的区别是什么:

Object var;

and:

Object* var = new Object();

?

5 个解决方案

#1


17  

Here you are creating var on the stack:

在这里,您将在堆栈上创建var:

Object var;

So in the above, var is the actual object.

所以在上面,var是实际的对象。


Here you are creating var on the heap (also called dynamic allocation):

在这里,您在堆上创建var(也称为动态分配):

Object* var = new Object()

When creating an object on the heap you must call delete on it when you're done using it. Also var is actually a pointer which holds the memory address of an object of type Object. At the memory address exists the actual object.

在堆上创建对象时,必须在使用它时调用delete。 var实际上也是一个指针,它保存Object类型对象的内存地址。在内存地址存在实际对象。


For more information: See my answer here on what and where are the stack and heap.

有关更多信息:请参阅我的答案,了解堆栈和堆的内容和位置。

#2


11  

This:

Object var();

Is a function declaration. It declares a function that takes no arguments and returns an Object. What you meant was:

是函数声明。它声明了一个不带参数的函数并返回一个Object。你的意思是:

Object var;

which, as others noted, creates a variable with automatic storage duration (a.k.a, on the stack).

正如其他人所指出的那样,它创建了一个具有自动存储持续时间的变量(a.k.a,在堆栈上)。

#3


5  

Object var();

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

声明返回Object的函数。要创建一个自动对象(即在堆栈上):

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

While:

Object* var = new Object();

Is a dynamically allocated Object.

是动态分配的对象。

#4


3  

Object var;

creates an Object that is valid until the end of the current scope, generally until the '}'

创建一个有效的Object,直到当前范围结束,通常直到'}'

Object* varp = new Object();

creates a pointed to a newly created Object. The pointer (varp) is valid until the en d of the scope, but the object itself exists until delete is called on the pointer.

创建一个指向新创建的Object。指针(varp)在作用域的en d之前有效,但是对象本身一直存在,直到在指针上调用delete。

You should always prefer the first version, unless there is a reason to do the second. The second version takes more memory (you need space for the pointer as well as the object), takes more time (the system needs to allocate memory on the heap), and you need to make sure to call delete to free the memory when you are done with it.

你应该总是喜欢第一个版本,除非有理由做第二个版本。第二个版本占用更多内存(你需要空间用于指针和对象),花费更多时间(系统需要在堆上分配内存),你需要确保调用delete以释放内存完成它。

#5


1  

Object var();

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

在堆栈上分配对象,因此您不必释放它

Object* var = new Object();

allocates object on the heap

在堆上分配对象

#1


17  

Here you are creating var on the stack:

在这里,您将在堆栈上创建var:

Object var;

So in the above, var is the actual object.

所以在上面,var是实际的对象。


Here you are creating var on the heap (also called dynamic allocation):

在这里,您在堆上创建var(也称为动态分配):

Object* var = new Object()

When creating an object on the heap you must call delete on it when you're done using it. Also var is actually a pointer which holds the memory address of an object of type Object. At the memory address exists the actual object.

在堆上创建对象时,必须在使用它时调用delete。 var实际上也是一个指针,它保存Object类型对象的内存地址。在内存地址存在实际对象。


For more information: See my answer here on what and where are the stack and heap.

有关更多信息:请参阅我的答案,了解堆栈和堆的内容和位置。

#2


11  

This:

Object var();

Is a function declaration. It declares a function that takes no arguments and returns an Object. What you meant was:

是函数声明。它声明了一个不带参数的函数并返回一个Object。你的意思是:

Object var;

which, as others noted, creates a variable with automatic storage duration (a.k.a, on the stack).

正如其他人所指出的那样,它创建了一个具有自动存储持续时间的变量(a.k.a,在堆栈上)。

#3


5  

Object var();

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

声明返回Object的函数。要创建一个自动对象(即在堆栈上):

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

While:

Object* var = new Object();

Is a dynamically allocated Object.

是动态分配的对象。

#4


3  

Object var;

creates an Object that is valid until the end of the current scope, generally until the '}'

创建一个有效的Object,直到当前范围结束,通常直到'}'

Object* varp = new Object();

creates a pointed to a newly created Object. The pointer (varp) is valid until the en d of the scope, but the object itself exists until delete is called on the pointer.

创建一个指向新创建的Object。指针(varp)在作用域的en d之前有效,但是对象本身一直存在,直到在指针上调用delete。

You should always prefer the first version, unless there is a reason to do the second. The second version takes more memory (you need space for the pointer as well as the object), takes more time (the system needs to allocate memory on the heap), and you need to make sure to call delete to free the memory when you are done with it.

你应该总是喜欢第一个版本,除非有理由做第二个版本。第二个版本占用更多内存(你需要空间用于指针和对象),花费更多时间(系统需要在堆上分配内存),你需要确保调用delete以释放内存完成它。

#5


1  

Object var();

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

在堆栈上分配对象,因此您不必释放它

Object* var = new Object();

allocates object on the heap

在堆上分配对象