【发布时间】:2011-04-10 02:16:38
【问题描述】:
在 C++ 中,
除了动态内存分配之外,以下两行代码在功能上是否有区别:
Time t (12, 0, 0); //t is a Time object
Time* t = new Time(12, 0, 0);//t is a pointer to a dynamically allocated Time object
我当然假设已经定义了 Time(int, int, int) ctor。我也意识到在第二种情况下 t 需要被删除,因为它是在堆上分配的。还有其他区别吗?
【问题讨论】:
-
它并不能真正算作一个答案,但除了已经给出的答案之外,您可能有兴趣知道,如果您想编写自己的内存管理,您可以覆盖 operator new/delete(为了更好性能)。
标签: c++ constructor new-operator