Value get changed even though I'm not using reference

Posted by atch on Stack Overflow See other posts from Stack Overflow or by atch
Published on 2010-04-05T14:52:56Z Indexed on 2010/04/05 15:03 UTC
Read the original article Hit count: 322

Filed under:
|

In code:

struct Rep
    {
        const char* my_data_;
        Rep* my_left_;
        Rep* my_right_;
        Rep(const char*);
    };

typedef Rep& list;

ostream& operator<<(ostream& out, const list& a_list)
    {
        int count = 0;
        list tmp = a_list;//----->HERE I'M CREATING A LOCAL COPY
        for (;tmp.my_right_;tmp = *tmp.my_right_)
        {
            out << "Object no: " << ++count << " has name: " << tmp.my_data_;
            //tmp = *tmp.my_right_;
        }
        return out;//------>HERE a_list is changed
    }

I've thought that if I'll create local copy to a_list object I'll be operating on completely separate object. Why isn't so?

Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about reference