C++ 指针对象与非指针对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2715198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:27:28  来源:igfitidea点击:

C++ Pointer Objects vs. Non Pointer Objects

c++pointersobject

回答by AnT

The reasons to use dynamic storage include (but probably not limited to)

使用动态存储的原因包括(但可能不限于)

  1. Manual control of the objects lifetime - the object will live until you explicitly destroy it
  2. Creating as many objects as necessary, when the final number of objects is only known at run-time (like number of nodes in a tree or number of elements in an array).
  3. Run-time control of the object's type (like actual type of polymorphic object).
  1. 手动控制对象生命周期 - 对象将一直存在,直到您明确销毁它
  2. 创建尽可能多的对象,当最终的对象数量仅在运行时才知道(例如树中的节点数或数组中的元素数)。
  3. 对象类型的运行时控制(如多态对象的实际类型)。

When it makes no difference, it is always better to create the object using your t3method. Don't use dynamic memory unless you have to. But sometimes you really have to (see the reasons above).

如果没有区别,最好使用您的t3方法创建对象。除非必须,否则不要使用动态内存。但有时你真的必须(见上面的原因)。

回答by Evan Teran

If you need an object to outlive the scope it was created in, then one of the solutions would be to create it on the Heap. In which case you would need a pointer. There are other reasons too, this is the most common.

如果您需要一个对象比创建它的作用域更长,那么解决方案之一就是在堆上创建它。在这种情况下,您将需要一个指针。还有其他原因,这是最常见的。

Another reason to use pointers is for "out" parameters. Granted you could use references, but many prefer to use pointers since it obviates the modification of the parameter at the call site. foo(var);vs foo(&var);

使用指针的另一个原因是用于“输出”参数。当然,您可以使用引用,但许多人更喜欢使用指针,因为它避免了在调用站点修改参数。foo(var);对比foo(&var);

Also, pointers can be used to either pass or return an object which may or may not exist. For example: T *foo(); // returns a pointer to an object or NULL if none exists.

此外,指针可用于传递或返回可能存在或可能不存在的对象。例如:T *foo(); // returns a pointer to an object or NULL if none exists

The list goes on and on really.

这个名单真的不胜枚举。

回答by i_am_jorf

If you have a very large object (for example one with very large buffers as members), you may not want to allocate it on the stack, since stack space is limited, in that case you allocate on the heap with operator new.

如果您有一个非常大的对象(例如,一个具有非常大缓冲区作为成员的对象),您可能不想在堆栈上分配它,因为堆栈空间是有限的,在这种情况下,您可以使用运算符 new 在堆上分配。

回答by Mashmagar

Any type of dynamic data structures (lists, binary trees, stacks, etc.) must use pointers to objects.

任何类型的动态数据结构(列表、二叉树、堆栈等)都必须使用指向对象的指针。

回答by Graphics Noob

The main difference is where it lives in memory. The "non pointer version" lives on the stack, meaning it will be invalid once the function returns, while the "pointer version" lives on the heap, which means it will be alive and well until somebody calls deleteon it. In general it is considered best practice to put objects on the stack whenever possible, and only on the heap when needed. A good example of needing the object on the heap would be something like this

主要区别在于它在内存中的位置。“非指针版本”存在于堆栈中,这意味着一旦函数返回它将无效,而“指针版本”存在于堆中,这意味着它会一直存在,直到有人调用delete它为止。一般来说,最好将对象尽可能放在堆栈上,并且仅在需要时才放在堆上。在堆上需要对象的一个​​很好的例子是这样的

Obj* f()
{
  return new Obj();
}

the new Obj()creates an Objobject on the heap and returns a pointer to it, which is then returned from the function.

在堆上new Obj()创建一个Obj对象并返回一个指向它的指针,然后从函数返回。

This, for example, would NOT work

例如,这行不通

Obj* f()
{
  Obj o1;
  return &o1;  //BAD!!
}

Since the pointer value &o1references memory on the stack, and f() is cleared off the stack at that point, who knows what will happen. Definitely nothing good.

由于指针值&o1引用堆栈上的内存,并且 f() 在那一刻从堆栈中清除,谁知道会发生什么。肯定没有什么好。

回答by Bill

A common (but not required) implementation is that local variables are allocated on the stack. The stack is finite, and it is possible to have an object be too large to allocate on the stack.

一个常见的(但不是必需的)实现是在堆栈上分配局部变量。堆栈是有限的,并且可能有一个对象太大而无法在堆栈上分配。