#include
#include
using namespace std;
The class String {
Friend ostream & amp; Operator<(ostream& , const String&);
Public:
String (const char *="");
String (const String & amp;);
~ the String ();
String operator () (int, int=0) const;
String operator++ (int);
Private:
int length;
Char * sPtr;
Void setString (const char *);
};
Ostream & amp; Operator<(ostream & amp; The output, const String & amp; S) {
Output
}
String: : String (const char * s) : length (s!=0? Strlen (s) : 0) {
Cout<" Calls the default constructor, general constructor, conversion constructors "
}
String: : String (const String & amp; Cpy) : length (cpy. Length) {
Cout<" Calls the copy constructor "& lt;
}
String: : ~ String () {
Cout<" The destructor call: "& lt;
}
String String: : operator () (int index, int subLenght) const {
If (index<0 | | index & gt;=length | | subLenght<0) return "";
int len;
If ((subLenght==0) | | (index + subLenght & gt; Length))
Len=length - index;
The else
Len=subLenght;
Char * tempPtr=new char [len + 1];
Strncpy (tempPtr, & amp; SPtr [index], len);
TempPtr [len]='\ 0';
String tempString (tempPtr);//triggers the transformation constructor here
The delete [] tempPtr;
Return tempString;//return triggered when copy constructor
}
{String String: : operator++ (int a)
String TMP (* this);//here trigger the copy constructor
+ + this - & gt; Length;
Return the TMP;//why don't like the one above the function returns when the trigger copy constructor
}
Void String: : setString (const char * string2) {
SPtr=new char [length + 1];
If (string2!=0)
Strcpy (sPtr, string2);
The else
SPtr [0]='\ 0';
}
Int main ()
{
String s1 (" 11111 ");
String s2 (" 22222 ");
Cout
}
CodePudding user response:
"C + + programming ideas"CodePudding user response:
I don't know what you are using is compiledVs2019 debug under both invoked in two copies both structure under the releas only off with a
Gcc4.9.2 in c + + 11 mode under the condition of open, calls a
Call a NRVO duplicate elimination, was the cause of vs deubg does not support NRVO support rvo, so under the debug two
GCC support nrvo by default, so once
CodePudding user response:
Is to use gcc5.1 is overloaded () when debugging return tempString constructor invokes the copy conforms to the copy, but when overloaded + + debug return TMP is also return objects, but I did not call the copy constructorCodePudding user response: