L Fundamentals:basic knowledge of C + +: Object-oriented features, constructors, destructors, dynamic bindings, etc., as well as common design patterns, UML diagrams
L C + + knowledge point: sizeof
L C + + knowledge points: Copy constructors,
L C + + recommended bibliography
Effective C + + is a good place to attack C + + before an interview, and the question that the interviewer likes to ask.
C + + Primer Comprehensive understanding of C + +
Inside C + + object model is immersed in understanding the interior of C + + objects. Can solve many problems of C + +.
The C + + programming Language A thorough mastery of C + +.
L C + + assignment operator function
Focus Point:
1) The return value type is a reference to that type, so that the value can be assigned consecutively
2) The type of the parameter passed in is declared as a constant reference, and a copy constructor is called from the formal parameter to the argument if it is not a reference.
3) If the instance itself has a heap of memory, do not forget to release the original memory, and in C + + we are accustomed to the practice is: the same type each instance of the internal heap memory is independent of each other, that is, the address is not the same, so do not forget to allocate new memory before releasing its own space
4) Remember to determine whether the parameter passed in and the current instance (*this) is the same instance that the address is the same, if the same is what does not do, different continue to replicate, this judgment is necessary when the instance itself has heap memory, because if the current instance and the incoming instance is the same instance, At the same time the instance has a heap of memory, if you do not judge to free the heap area memory, then delete the incoming instance of the heap area of memory is deleted, there is no way to assign a value.
#include <iostream>#include<string.h>//#include <bad_alloc.h>classcmystring{ Public: CMyString (Char* PData =NULL); CMyString (ConstCMyString & ); CMyString&operator=(ConstCMyString & ); ~cmystring (); Private: Char*m_pdata;}; Cmystring::cmystring (Char*pData) {M_pdata=NULL; M_pdata=New Char[Strlen (PData) +1]; //if (!m_pdata) throw bad_alloc;//I don't know how to use it here.strcpy (m_pdata,pdata);} Cmystring::cmystring (ConstCMyString &str) {cmystring (str.m_pdata);//do not know how; /*m_pdata = NULL; M_pdata = new Char[strlen (str.m_pdata) + 1]; if (!m_pdata) throw bad_alloc;//is not known here how to use strcpy (M_pdata,pdata); */}/*Version One*///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;//} /*version Two*/cmystring& cmystring::operator=(ConstCMyString &str) { if( This= = &str)return* This; CMyString strtmp (str);//call the copy constructor, which is actually the function of new, but also handle the exception situation; Char* Chtmp =Strtmp.m_pdata; Strtmp.m_pdata= M_pdata;//This can use the destructor of the local object strtmp to delete the original stack area of the object;M_pdata =chtmp; return* This; }cmystring::~cmystring () {}intMain () {return 0;}
C + + knowledge points