I can't seem to get my head around functions which return a reference, what is it a reference to? It seems they are commonly used when writing operators. Here's a couple of examples I've found.
The first is the assignment operator from Steve Heller's string class
The second is taken from this well known pointer tutorialCode:string& string::operator = (const string& Str) { char* temp = new char[Str.m_Length]; m_Length = Str.m_Length; memcpy(temp,Str.m_Data,m_Length); delete [ ] m_Data; m_Data = temp; return *this; }
In both cases I just can't see why we need to return a reference, or even what it's a reference to.Code:class array { 4 int base[5]; 5 public: 6 int& operator[] ( int i ) 7 { 8 return base[i]; 9 } 10 };
Regards,
Stonehambey



LinkBack URL
About LinkBacks




