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`
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01