指针分配与普通声明

发布于 2024-08-22 02:17:25 字数 240 浏览 6 评论 0原文

有时我在各种 C++ 程序中看到,像这样声明和使用对象:

object *obj = new object;
obj->action();
obj->moreAction();
//etc...

这样做有什么好处,而不是简单地这样做:

object obj;
obj.action();
obj.moreAction();
//etc

sometimes I see in various C++ programs, objects declared and used like so:

object *obj = new object;
obj->action();
obj->moreAction();
//etc...

Is there any benefit of doing that, instead of simply doing:

object obj;
obj.action();
obj.moreAction();
//etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迟月 2024-08-29 02:17:25

是的 - 您可以将指针存储在容器中或从函数中返回它,并且当指针超出范围时该对象不会被销毁。指针用于

  • 避免不必要的对象复制、
  • 促进可选对象创建、
  • 自定义对象生命周期管理、
  • 创建复杂的类似图形的结构、
  • 以及上述的组合。

这并不是免费的 - 当您不再需要该对象时,您需要手动销毁该对象(删除),并且决定何时到来并不总是那么容易,而且您可能只是忘记对其进行编码。

Yes - you can store the pointer in a container or return it from the function and the object will not get destroyed when the pointer goes out of scope. Pointers are used

  • to avoid unnecessary copying of object,
  • to facilitate optional object creation,
  • for custom object lifetime management,
  • for creating complex graph-like structures,
  • for the combinations of the above.

This doesn't come for free - you need to destroy the object manually (delete) when you no longer need it and deciding when this moment comes is not always easy, plus you might just forget to code it.

咽泪装欢 2024-08-29 02:17:25

第一种形式是在堆上分配对象,让您可以完全控制(并承担全部责任)对象的生存时间:您必须删除obj明确。

在第二种形式中,当 obj 超出分数(离开当前代码块时)时,该对象将被自动且不可撤销地销毁。

The first form, allocating objects on the heap, gives you full control of (and full responsibility for) the object's live time: you have to delete obj explicitly.

In the second form, the object is automatically and irrevocably destroyed when obj goes out of score (when leaving the current code block).

迷路的信 2024-08-29 02:17:25

还有一个无人提及的原因。

堆栈通常为1Mb,因此创建大对象必须在堆上完成(使用new)

One other reason no-one has mentioned.
The stack is typically 1Mb, so creating large objects must be done on the heap ( with new)

枯叶蝶 2024-08-29 02:17:25

基本上,如果您希望对象的生存期超出您创建它的范围的生命周期,则应该仅使用“new”。例如:

X* g(int i) { /* ... * /返回新的X(i); } // X 的寿命比 g() 的调用长

如果您希望对象仅存在于作用域中,请不要使用“new”,而只需定义一个变量:

{
    ClassName x;
    // use x
}

Basically, you should only use "new" if you want an object to live beyond the lifetime of the scope you create it in. For eg:

X* g(int i) { /* ... */ return new X(i); } // the X outlives the call of g()

If you want an object to live in a scope only, don't use "new" but simply define a variable:

{
    ClassName x;
    // use x
}
心清如水 2024-08-29 02:17:25

归根结底是对相关对象的生命周期有更多的控制(当使用new时)。

It comes down to having more control over the lifecycle of the object in question (when using new).

一口甜 2024-08-29 02:17:25

是的,有一个很好的理由:使用后一种形式时,您有更多机会获得正确的程序。

问题是前一种形式(指针)是 Cism,在 中在 C++ 中,您应该使用智能指针来确保对象在其生命周期结束时正确销毁。

现在,如果您使用 std::auto_ptr; obj(new Object()); 你有 3 个好处:

  1. 你现在显式地管理对象的生命周期(但不能忘记它)
  2. 你可以将对象存储到具有多态性的容器中
  3. 你不会阻塞堆栈,因此遇到堆栈溢出的风险较小

Yes there is a good reason: you have much more chance to have a correct program when using the latter form..

The problem is that the former form (pointer) is a Cism, in C++ you should use a smart pointer to ensure proper destruction of the object at the end of its life.

Now, if you use std::auto_ptr<Object> obj(new Object()); you have 3 benefits:

  1. you now manage the life cycle of your object explicitly (but cannot forget it)
  2. you can store you object into a container with polymorphism
  3. you do not clog the stack, and thus have less risk of running into a stack overflow
三人与歌 2024-08-29 02:17:25

人们可以用相反的方式问:你什么时候应该使用奇怪第一个选项?基本上,如果您想分配大对象,因为如果您不必这样做并且可以将其放在堆栈上,那么这将是更快的选择:这是使用 C++ 相对于 JAVA 的主要优点之一,JAVA 将所有对象放在一起在堆上。在处理许多小对象的分配时,这种好处尤其明显:将它们放在堆栈上以提高速度。取消引用指针会产生成本开销。您可以在此处找到有关 boost 池库的信息它为我们提供了管理此类分配的工具。

one can ask in a opposite way: when should you use strange first option? basically if you want to allocate big object, because if you don't have to do it and you can put it on the stack it will be much faster option: this is one of main advantages of using C++ over JAVA, which puts all objects on the heap. and this benefit is specially true when dealing with many, many allocations of little objects: put them on stack to increase speed. there is cost overhead of dereferencing pointer. you can find here info about boost pool library which provides us with tools to manage such allocations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文