Visit the private variable

xiaoxiao2021-03-06  52

Today, I came back to the interview, bring back a question, huh, huh.

Given a string class, the members, and functions of the class have been defined. Now require, according to the definition, the implementation of each function is written. The definition of the class is as follows: CLASS CMYSTRING {public: CMYSTRING (const cmystring & a_cother); // Configure function, making the member properties of the constructor as the same as A_Cother, (may wish to set the function 1) CMYSTRING (char * a_pcstr = null); // Configuration function, copy string a_pcstr to m_pcdata; (function 2) CMYSTRING & OPERATOR = (const cmystring & a_cother); // Equation Operator (Function 3) Virtual ~ CMYSTRING (); // Destructor (Function 4) PRIVATE: CHAR * m_PCDATA;

The situation now encountered is that the function 2 and function 4 are easily written, but the function 1 and function 3 are more difficult. I am roughly, this is not easy, three down five resolution. The implementation of the program is as follows:

CMYSTRING :: CMYSTRING (CHAR * A_PCSTR) {if (a_pcstr == null) {m_pcdata = null;} else {INT Ilen; Ilen = Strlen (a_pcstr); m_pcdata = (char *) Malloc (sizeof (char) * (Ilen) 1)); MEMSET (m_pcdata, '/ 0', sizeof (char) * (ilen 1)); struffpy (m_pcdata, a_pcstr);}}

CMYSTRING :: CMYSTRING (const cmystring & a_cother) {memcpy (this, & a_cother, sizeof (cmystring));}

CMYSTRING & CMYSTRING :: Operator = (const cmystring & a_cother) {cmystring * ptmpstr; ptmpstr = new cmystring (a_cother); return * ptmpstr;}

CMYSTRING :: ~ cmystring () {if (m_pcdata! = Null) {free (m_pcdata); m_pcdata = null;}}

In order to test, the test procedure is written, as follows: char ctmp [] = "helloworld"; cmyString stra (CTMP); cmystring strb (stra); cmystring strc = strb;

It is found that the implementation of the function is not wrong, huh, huh, complete, ok! But in a careful look, wrong, how straighted, strb.m_pcdata, and strc.m_pcdata points to the same address! Oh, yes, in the function 1 and function 3, I only copied the pointer of the private member M_PCData, so the private variable M_PCData of the three objects of this class points to the same address. It turns out that my method is wrong. Of course, the correctness of the function 2 and function 4 is uninouposed. Later, a colleague finally made it. Function 1 and Functions 3 code as follows: cmystring :: cmystring (const cmystring & a_cother) {if (a_cother.m_pcdata == null) {m_pcdata = null;} else {INT Ilen; Ilen = Strlen (a_cother.m_pcdata); m_pcdata = Char *) Malloc (Ilen 1)); MEMSET (M_PCData, '/ 0', SizeOf (CHAR) * (Ilen 1)); struffpy (m_pcdata, a_cother.m_pcdata);}}

CMyString & CMyString :: operator = (const CMyString & a_cOther) {if (a_cOther.m_pcData == NULL) {m_pcData = NULL;} else {int iLen; iLen = strlen (a_cOther.m_pcData); if (m_pcData = NULL) {m_pcData = (Char *) Malloc (Ilen 1));} else {m_pcdata = (char *) Realloc (m_pcdata, sizeof (char) * (ilen 1));} Memset (m_pcdata, ' / 0 ', SizeOf (CHAR) * (Ilen 1)); struffpy (m_pcdata, a_cother.m_pcdata);} return * this;}

转载请注明原文地址:https://www.9cbs.com/read-114954.html

New Post(0)