你发现这个 C++ 有什么问题吗?代码?
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
public:
int *p;
A()
{
p =new int;
}
~A()
{
delete p; //Is this what i am doing is correct?
cout << "in A's destructor"<<endl;
}
};
int main()
{
A *obj=new A;
delete obj;
getch();
}
这个程序,我已经在 Dev c++ 中执行,并且编译和执行得很好。 但我怀疑这不太好。特别是在析构函数中,我说delete P
,
我错了吗?
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
public:
int *p;
A()
{
p =new int;
}
~A()
{
delete p; //Is this what i am doing is correct?
cout << "in A's destructor"<<endl;
}
};
int main()
{
A *obj=new A;
delete obj;
getch();
}
This programs,i have executed in Dev c++ and compiles and executes fine.
But i doubt this is not fine.Specially in the destructor where i say delete P
am i wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该代码在逻辑上很好(您所询问的
new
/delete
部分),但在其他方面设计得很糟糕。首先,如果
class A
拥有堆分配的int
(int
的生命周期与class A
对象的生命周期相同) )用 new 创建它是没有意义的,将其作为 A 类的成员变量会更有效。其次,您不应该将其公开 - 这会破坏封装并允许大量滥用。最后,您仍然允许但未实现复制构造函数和赋值运算符,因此每当复制类对象时,您都会获得浅复制。您应该适当地实施它们或禁止它们。
不过,
删除
使用new
分配的变量的部分完全没问题。That code is logically fine (the part with
new
/delete
you're asking about), but designed badly in other aspects.First, if
class A
owns that heap-allocatedint
(int
lives for as long asclass A
object lives) there's no point in creating it withnew
, it'd be much more efficient to make it a member variable ofclass A
. Second, you shouldn't make itpublic
- that breaks encapsulations and allows lots of abuse.Finally, you still have copy constructor and assignment operator allowed and unimplemented, so any time a class object is copied you get shallow copy. You should either implement them appropriately or prohibit them.
The part where you
delete
a variable allocated withnew
is completely fine however.我发现此代码有 4 个问题:
new
的两次使用delete
的两次使用可能是您剪下的精简代码在某种程度上缺乏原始实现,或者您来了来自一种一切都是新的语言,但其代码远非惯用的 C++。
惯用的重写:
I see 4 issues with this code:
new
delete
It may be that your reduced code snipped is somewhat lacking wrt the original implementation, or that you come from a language where everything is
new
'ed, but the code as is is far from being idiomatic C++.The idiomatic rewrite:
正如 Sharptooth 指出的那样,在 dtor 中使用 delete 是完全有效的。 delete null 在 std 中定义为 noop。
但是,对于这样的事情,请考虑使用shared_ptr或类似的东西......
hth
Mario
As sharptooth pointed out, using delete in the dtor is completely valid. delete null is defined in the std as noop.
however, for things like this, consider using a shared_ptr or something similar...
hth
Mario
在这种情况下,您还需要定义自己的复制构造函数和赋值运算符(或将它们声明为私有以使类不可复制);默认的复制构造函数和赋值运算符执行浅复制。也就是说,它们复制指针,而不是指针的内容。因此,如果您这样做:
xp
和yp
都指向同一位置,因此当它们被破坏时,它们会尝试释放相同的内存,这会产生未定义的行为。要解决此问题,请定义您自己的复制构造函数和复制 int 的赋值运算符:
You also need to define your own copy constructor and assignment operator (or declare them as private to make the class as noncopyable) in this case; the default copy constructor and assignment operator do a shallow copy. That is, they copy the pointer, not the contents of the pointer. So if you do this:
Both
x.p
andy.p
point to the same location, hence when they're destructed they try to free the same memory, which yields undefined behavior.To fix this, define your own copy constructor and assignment operator that copies the int: