Mark Van Peteghem <[EMAIL PROTECTED]> wrote:
> The purpose of this is that you can use more than assignment at a 
>time, 
> just like with primitive types:
> 
>    form1 a, b, c;
>    a = b = c;
> 
> Remember that operator= is always evaluated from right to left, so 
>it's 
> equivalent to a = (b = c), so first b = c is done. This returns a 
> reference to b, if the implementation ends with return *this. Then a 
>= b 
> is done.
> If you don't return a reference, like in form1, it probably would 
>work 
> as well, but slower, because then a copy is returned every time.
> 
> -- 
>  Mark Van Peteghem
>  http://www.q-mentum.com -- easier and more powerful unit testing


hiii,

template <class _tipe>
class Item
{
public:
    Item() {
       mpData = NULL;
    }

    Item &operator=(const _tipe &rNewData) {     //FIXME!!!
        setData(rNewData);
        return *this;
    }
        
    void setData(const _tipe &rNewData) {
        if (mpData != NULL)
           delete mpData;
        mpData = new _tipe(rNewData);
    }
        
    _tipe getData() const {
        return (*mpData);
     }
  
     virtual ~Item() {
        if (mpData != NULL)
           delete mpData;
     }


private:
     _tipe *mpData;
};


int main()
{
    Item<int> a;
    a = 4;
  
    cout << a.getData() << endl;
    return 0;
}


the code above will output: 4
but when i not use reference on operator=, the result will be 0
why does this happen?

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


thanx and regards
=============================================
Netkuis Instan untuk wilayah Bandung (kode area 022) - SD,SMP,SMA
Berhadiah total puluhan juta rupiah... periode I dimulai 1 April 2004
=============================================





------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/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=3773
    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