erudeuin wrote:

> template <class _tipe>
> class Item
> {
> public:
>     Item() {
>        mpData = NULL;
>     }
>
>     Item &operator=(const _tipe &rNewData) {     //FIXME!!!
>         setData(rNewData);
>         return *this;
>     }

If you write Item operator=(const _tipe &rNewData), a copy is returned 
instead of a reference. Because you defined no copy constructor, the 
default copy constructor is used, which simply copies the memory that 
the object uses. So a new Item<int> object is returned, with the same 
pointer. Because you don't use the return value, it is deleted 
immediately, so the destructor deletes the pointer. But your object 'a' 
has the same pointer, so that is deleted, and probably set to 0 by your 
compiler.

It is best to always return a reference. It is safer and faster.

> [...]

> is that my code above safe(especially memory leak)
> could i assign int value for 'a object' repeatedly, for instance
>     a = 5;
>     a = 6; //and etc, will it be memory leak

I tried it in MSVC++, which has good memory leak detection, and it 
detected no memory leaks, even with the extra a=5 and a=6.

-- 
  Mark Van Peteghem
  http://www.q-mentum.com -- easier and more powerful unit testing



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe : [EMAIL PROTECTED]

 
Yahoo! Groups Links

<*> To reply to this message, go to:
    http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=3777
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/Programmers-Town/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to