http://blog.csdn.net/aaron25/article/details/708852
Https://www.cnblogs.com/LUO77/p/5771237.html
Https://www.cnblogs.com/yjd_hycf_space/p/7495640.html
http://blog.csdn.net/ljzcome/article/details/574158
Https://www.cnblogs.com/Y1Focus/p/6707121.html
Cond
#include <QCoreApplication> #include <QTextStream> #include <iostream> using namespace std; Class String {public:string (const char *str=null); Normal constructor string (const string &other); Copy constructor ~string (void); destructor String & operator= (const string &other);//Assignment function Private:char *m_string;
Private member, save string};
* * Normal Constructor * constructor first creates a string object based on a string constant.
* This constructor first allocates enough memory and then copies this string constant to this memory/string::string (const char *str) {cout<< Normal constructor <<endl; if (str==null)//If Str is empty, save the empty string {m_string=new char[1]; Allocating a byte if (m_string==null) {//memory to request a successful std::cout<< "failed to request memory."
"<<std::endl;
Exit (1); } *m_string= ';
Assign the value to the string terminator} else {m_string=new char[strlen (str) +1];//Allocate space to accommodate STR content if (m_string==null) {///whether the memory applies ToWork std::cout<< "failed to request memory.
"<<std::endl;
Exit (1); } strcpy (M_STRING,STR); Assign STR to private member}/* * Copy constructor * All user-defined types that need to allocate system resources need a copy constructor * It can pass a string parameter in a function call and it is implemented when a function returns a String object in a worthwhile fashion
Copy on return */string::string (const String &other) {cout<< copy constructor <<endl;
M_string=new Char[strlen (other.m_string) +1]; if (m_string==null) {//memory requested successfully std::cout<< "failed to request memory."
"<<std::endl;
Exit (1);
} strcpy (m_string,other.m_string); /* * Defines a destructor for the way memory leaks, and when a String object exceeds its scope, the destructor releases the memory it occupies/string::~string (void) {cout<< destructor <<endl
; if (m_string!=null)//If m_string is not null, free heap memory {delete [] m_string;
After release, null m_string=null is placed;
}/* * Assignment function implements string's value-passing activity/string & string::operator = (const string &other) {cout<< "assignment function" <<endl; if (this==&other)//If the object is the same as the otherobject {return *this;
return directly to itself} delete [] m_string;
M_string=new Char[strlen (other.m_string) +1]; if (m_string==null) {//memory requested successfully std::cout<< "failed to request memory."
"<<std::endl;
Exit (1);
} strcpy (m_string,other.m_string);
return *this;
int main (int argc, char *argv[]) {qcoreapplication A (argc, argv);
printf ("mystring");
GetChar (); String stra ("Hello"); Invoke normal constructor String strb ("word"); Invokes the ordinary constructor String strc (Stra); Call the copy constructor strc=strb;
Call assignment function Qtextstream Qin (stdin);
Qtextstream Qout (stdout);
QString Qstr;
qin>>qstr;
qout<<qstr<<endl;
return A.exec (); }