Re: 答复: [cpp] 初次光临,请多指教
Stone Jiang
2005119 at gmail.com
Wed Jul 12 15:42:33 CST 2006
的确是缺少copy constructor和copy assignment导致的问题,
当对象复制的时候,不会内存泄漏,但会指针悬挂, 在赋值操作的时候,目标对象内部的指针指向的动态分配的内存块失去控制.
xWrapper a ,b;
当a = b的时候,a.xItex = b.xItem,指向的同时相同的地址.
当a.xItem变化是b.xItem也跟着变化.
当a退出作用范围的时候,b.xItem已被系统收回.这时使用b.xItem 可能会系统崩溃.
记住一点,当对象有指针作为成员变量的时候,一定要考虑对象赋值是否必要,如果需要,那copy constructor和copy
assignment不能省略.
需要的时候还要重载new/delete运算符.
On 7/12/06, 孟岩 <myan at csdn.net> wrote:
>
>
>
>
>
> 把你崩溃的程序贴上来。
>
>
>
> 应该就是由于缺少copy constructor和copy assignment导致的问题而已。
>
>
>
> ________________________________
>
> 发件人: cpp-bounces at codingnow.com [mailto:cpp-bounces at codingnow.com] 代表 孙华明
> 发送时间: 2006年7月10日 16:09
> 收件人: C++ Discuss Group
> 主题: Re: [cpp] 初次光临,请多指教
>
>
>
>
> 是啊怎么崩溃的?
>
>
> 没有内存泄露啊,好像也没有空指针访问啊。
>
>
>
> ----- Original Message -----
>
>
> From: ling
>
>
> To: C++ Discuss Group
>
>
> Sent: Monday, July 10, 2006 10:19 AM
>
>
> Subject: [cpp] 初次光临,请多指教
>
>
>
>
>
> 今天看了c++对话系列,为什么说下面的类程序会因为内存问题而崩溃呢?
>
>
>
>
>
> #include "xStruct.h" // definition of struct X
>
> class xWrapper
> {
> X* xItem;
> public:
> xWrapper() : xItem(new X) { }
> ~xWrapper() { delete xItem; }
>
> void dump() { /* dumps xItem to cout */ }
> };
> ________________________________
>
> _______________________________________________
> Cpp mailing list
> Cpp at codingnow.com
> http://codingnow.com/mailman/listinfo/cpp
> ________________________________
Confidentiality Notice: The information contained in this e-mail and any
accompanying attachment(s) is intended only for the use of the intended
recipient and may be confidential and/or privileged of Neusoft Group Ltd.,
its subsidiaries and/or its affiliates. If any reader of this communication
is not the intended recipient, unauthorized use, forwarding, printing,
storing, disclosure or copying is strictly prohibited, and may be unlawful.
If you have received this communication in error, please immediately notify
the sender by return e-mail, and delete the original message and all copies
from your system. Thank you. ________________________________
> _______________________________________________
> Cpp mailing list
> Cpp at codingnow.com
> http://codingnow.com/mailman/listinfo/cpp
>
>
--
Take care
Stone Jiang
blog: http://blog.csdn.net/FocusOnACE/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codingnow.com/pipermail/cpp/attachments/20060712/5de42225/attachment.html
More information about the Cpp
mailing list