<分区>
Possible Duplicate:
What is difference between instantiating an object using new vs. without
Creating an object: with or without `new`
这两个语句有什么区别
HttpUtil httpUtil;
和
HttpUtil *net = new HttpUtil();
用哪个比较好?
<分区>
Possible Duplicate:
What is difference between instantiating an object using new vs. without
Creating an object: with or without `new`
这两个语句有什么区别
HttpUtil httpUtil;
和
HttpUtil *net = new HttpUtil();
用哪个比较好?
最佳答案
第一个创建一个自动变量。内存管理是自动的,分配速度更快,因为它在堆栈上,没有内存泄漏的可能性。
第二个版本创建了一个动态变量——您负责清理内存并且在堆上分配速度较慢。
总是喜欢第一个。如果您必须使用动态分配(用于持续生命周期或多态行为),请使用智能指针而不是原始指针。
关于c++ - 对象构造语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13932287/