Creating an object: with or without `new`(创建一个对象:有或没有`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 new
d 是 C 风格的字符串;它来自一个字符串文字.delete
ing 这是一个错误(虽然不会产生编译错误,但会在运行时产生不可预测的行为).
Your original code is broken, as it delete
s a char
array that it did not new
. In fact, nothing new
d the C-style string; it came from a string literal. delete
ing 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 delete
ing anything that it didn't itself new
. This behaviour should be well-documented. In this case, the rule is being completely broken.
这篇关于创建一个对象:有或没有`new`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:创建一个对象:有或没有`new`


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01