创建对象:带或不带`new` [英] Creating an object: with or without `new`

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

问题描述


可能重复:



$使用新对象和不使用实例化对象之间的区别是什么? b

$ b

这可能是一个基本问题,可能已经被问过(例如,这里);但我还是不明白。



考虑下面的C ++类:

  class Obj {
char * str;
public:
Obj(char * s){
str = s;
cout<< str;
}
〜Obj(){
cout<< Done!\\\
;
delete str; //看下面的Loki Astari的注释为什么这行代码是不好的做法
}
};

以下代码段之间有什么区别:

  Obj o1(Hi\\\
);

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

为什么前者调用析构函数,但是后者没有明确调用 destroy )?



哪一个是首选?



首先使用自动存储持续时间创建一个对象。当当前块( {...} )结束时,它被创建,使用,然后超出范围。这是创建对象的最简单的方法,与写 int x = 0;



第二个创建具有动态存储持续时间的对象,并允许两种操作:




  • 对象的生命周期,因为它不会自动超出范围;您必须使用关键字 delete 明确销毁它;


  • ,因为对象创建发生在运行时。 (我不会在这里分析动态数组的细节。)






使用前者,除非你需要使用后者。



你的C ++书应该很好地覆盖。如果您没有帐户,请不要进一步,除非您已经购买并阅读了几次其中之一



祝你好运。






您的原始代码已损坏,因为 delete sa char > new 。事实上,没有 新的 d C风格的字符串;它来自一个字符串字面量。 delete 这是一个错误(虽然不会产生编译错误,但在运行时不可预测的行为)。



通常,一个对象不应该有 delete 任何不是自己 new 的责任。这种行为应该被详细记录。在这种情况下,规则被完全打破。


Possible Duplicate:
What is difference between instantiating an object using new vs. without

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.

Consider the following C++ class:

class Obj{
    char* str;
public:
    Obj(char* s){
        str = s;
        cout << str;
    }
    ~Obj(){
        cout << "Done!\n";
        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\n");

and

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

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

Which one is preferred?

解决方案

Both do different things.

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:

  • Fine control over the lifetime of the object, since it does not go out of scope automatically; you must destroy it explicitly using the keyword 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.

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.

Good luck.


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).

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`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆