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

2021-12-30 00:00:00 constructor destructor c++ new-operator

可能的重复:
使用 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.

相关文章