pointers - C++ interview about operator -
here code implementing = assignment class named cmystring
, , code right.
cmystring& cmystring::operator =(const cmystring &str) { if(this == &str) return *this; delete []m_pdata; m_pdata = null; m_pdata = new char[strlen(str.m_pdata) + 1]; strcpy(m_pdata, str.m_pdata); return *this; }
the instance passed reference, , first 'if' checking whether instance passed in or not. question is: why use &str
compare, doesn't str
contain address of instance? 1 explain how line works?
also, want make sure this
contains address of object: correct?
address-of operator , reference operator different.
the & used in c++ reference declarator in addition being address-of operator. meanings not identical.
int target; int &rtarg = target; // rtarg reference integer. // reference initialized refer target. void f(int*& p); // p reference pointer
if take address of reference, returns address of target. using previous declarations, &rtarg same memory address &target.
Comments
Post a Comment