<tfoot id='ZnuK0'></tfoot>
<i id='ZnuK0'><tr id='ZnuK0'><dt id='ZnuK0'><q id='ZnuK0'><span id='ZnuK0'><b id='ZnuK0'><form id='ZnuK0'><ins id='ZnuK0'></ins><ul id='ZnuK0'></ul><sub id='ZnuK0'></sub></form><legend id='ZnuK0'></legend><bdo id='ZnuK0'><pre id='ZnuK0'><center id='ZnuK0'></center></pre></bdo></b><th id='ZnuK0'></th></span></q></dt></tr></i><div id='ZnuK0'><tfoot id='ZnuK0'></tfoot><dl id='ZnuK0'><fieldset id='ZnuK0'></fieldset></dl></div>
  • <legend id='ZnuK0'><style id='ZnuK0'><dir id='ZnuK0'><q id='ZnuK0'></q></dir></style></legend>
      <bdo id='ZnuK0'></bdo><ul id='ZnuK0'></ul>

      <small id='ZnuK0'></small><noframes id='ZnuK0'>

      1. 创建一个对象:有或没有`new`

        时间:2023-09-27

          <tbody id='Qa3S8'></tbody>
          <bdo id='Qa3S8'></bdo><ul id='Qa3S8'></ul>
          • <tfoot id='Qa3S8'></tfoot>
            <legend id='Qa3S8'><style id='Qa3S8'><dir id='Qa3S8'><q id='Qa3S8'></q></dir></style></legend>

              1. <i id='Qa3S8'><tr id='Qa3S8'><dt id='Qa3S8'><q id='Qa3S8'><span id='Qa3S8'><b id='Qa3S8'><form id='Qa3S8'><ins id='Qa3S8'></ins><ul id='Qa3S8'></ul><sub id='Qa3S8'></sub></form><legend id='Qa3S8'></legend><bdo id='Qa3S8'><pre id='Qa3S8'><center id='Qa3S8'></center></pre></bdo></b><th id='Qa3S8'></th></span></q></dt></tr></i><div id='Qa3S8'><tfoot id='Qa3S8'></tfoot><dl id='Qa3S8'><fieldset id='Qa3S8'></fieldset></dl></div>
                • <small id='Qa3S8'></small><noframes id='Qa3S8'>

                  本文介绍了创建一个对象:有或没有`new`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  使用 new 和. 没有

                  这可能是一个基本问题,并且可能已经被问过(例如,此处);但我还是不明白.所以,让我问一下.

                  This is probably a basic question, and might have already been asked (say, here); yet I still don't understand it. So, let me ask it.

                  考虑以下 C++ 类:

                  Consider the following C++ class:

                  class Obj{
                      char* str;
                  public:
                      Obj(char* s){
                          str = s;
                          cout << str;
                      }
                      ~Obj(){
                          cout << "Done!
                  ";
                          delete str;        // See the comment of "Loki Astari" below on why this line of code is bad practice
                      }
                  };
                  

                  以下代码片段有什么区别:

                  what's the difference between the following code snippets:

                  Obj o1 ("Hi
                  ");
                  

                  Obj* o2 = new Obj("Hi
                  ");
                  

                  为什么前者调用析构函数,而后者不调用(没有显式调用delete)?

                  Why the former calls the destructor, but the latter doesn't (without explicit call to delete)?

                  哪个更受欢迎?

                  推荐答案

                  两者都做不同的事情.

                  第一个创建一个具有自动存储持续时间的对象.它被创建、使用,然后在当前块 ({ ... }) 结束时超出范围.这是最简单的创建对象的方式,和你写int x = 0;

                  The first creates an object with automatic storage duration. It is created, used, and then goes out of scope when the current block ({ ... }) ends. It's the simplest way to create an object, and is just the same as when you write int x = 0;

                  第二个创建一个具有动态存储期的对象,并允许做两件事:

                  The second creates an object with dynamic storage duration and allows two things:

                  • 精细控制对象的生命周期,因为它不会自动超出范围;您必须使用关键字 delete;

                  创建大小仅在运行时已知的数组,因为对象创建发生在运行时.(我不会在这里讨论分配动态数组的细节.)

                  Creating arrays with a size known only at runtime, since the object creation occurs at runtime. (I won't go into the specifics of allocating dynamic arrays here.)

                  两者都不是首选;这取决于你在做什么,哪个最合适.

                  Neither is preferred; it depends on what you're doing as to which is most appropriate.

                  除非您需要使用后者,否则请使用前者.

                  Use the former unless you need to use the latter.

                  你的 C++ 书应该很好地涵盖了这一点.如果您没有,请不要再,直到您购买并阅读多次其中之一.

                  Your C++ book should cover this pretty well. If you don't have one, go no further until you have bought and read, several times, one of these.

                  祝你好运.

                  您的原始代码已损坏,因为它delete 是一个 char 数组,它不是 new.事实上,nothing newd 是 C 风格的字符串;它来自一个字符串文字.deleteing 这是一个错误(虽然不会产生编译错误,但会在运行时产生不可预测的行为).

                  Your original code is broken, as it deletes a char array that it did not new. In fact, nothing newd the C-style string; it came from a string literal. deleteing that is an error (albeit one that will not generate a compilation error, but instead unpredictable behaviour at runtime).

                  通常一个对象不应该负责删除任何不是它本身new的东西.这种行为应该有据可查.在这种情况下,规则完全被打破了.

                  Usually an object should not have the responsibility of deleteing anything that it didn't itself new. This behaviour should be well-documented. In this case, the rule is being completely broken.

                  这篇关于创建一个对象:有或没有`new`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是 C++ 中的复制构造函数? 下一篇:内置类型(int、char 等)的析构函数

                  相关文章

                    <legend id='PkFrb'><style id='PkFrb'><dir id='PkFrb'><q id='PkFrb'></q></dir></style></legend>
                    <i id='PkFrb'><tr id='PkFrb'><dt id='PkFrb'><q id='PkFrb'><span id='PkFrb'><b id='PkFrb'><form id='PkFrb'><ins id='PkFrb'></ins><ul id='PkFrb'></ul><sub id='PkFrb'></sub></form><legend id='PkFrb'></legend><bdo id='PkFrb'><pre id='PkFrb'><center id='PkFrb'></center></pre></bdo></b><th id='PkFrb'></th></span></q></dt></tr></i><div id='PkFrb'><tfoot id='PkFrb'></tfoot><dl id='PkFrb'><fieldset id='PkFrb'></fieldset></dl></div>
                    • <bdo id='PkFrb'></bdo><ul id='PkFrb'></ul>

                    <small id='PkFrb'></small><noframes id='PkFrb'>

                  1. <tfoot id='PkFrb'></tfoot>