Home > Back-end >  Consult, overloading () and operator, () have two copy constructor, and only one time?
Consult, overloading () and operator, () have two copy constructor, and only one time?

Time:01-16


#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) {
OutputReturn the output;
}
String: : String (const char * s) : length (s!=0? Strlen (s) : 0) {
Cout<" Calls the default constructor, general constructor, conversion constructors "
SetString (s);
}
String: : String (const String & amp; Cpy) : length (cpy. Length) {
Cout<" Calls the copy constructor "& lt; SetString (cpy. SPtr);
}
String: : ~ String () {
Cout<" The destructor call: "& lt; The delete [] sPtr;
}
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 ");
CoutCout
}

CodePudding user response:

"C + + programming ideas"

CodePudding user response:

I don't know what you are using is compiled
Vs2019 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 constructor

CodePudding user response:

reference 1/f, zhao teacher reply:
"c + + programming ideas"

Miss zhao, I know is that there are instructions which chapter, a book, it's not easy to find the
Said I was reading a book on the return if the object is, if not copy reference words is called structure of function, but the result is different here

CodePudding user response:

reference 4 floor xiexiejohn response:
Quote: reference, 1/f, zhao teacher reply:
"c + + programming ideas"

Miss zhao, I know is that there are instructions which chapter, a book, it's not easy to find the
Said I was reading a book on the return if the object is, if not copy reference words is called structure of function, but the results are different here

CodePudding user response:

refer to the second floor truth is right or wrong response:
I don't know what you are using is compiled
Vs2019 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 a


Online search NRVO duplicate elimination, know is a way of optimization, but the same file within the function, how one can use NRVO duplicate elimination, another is not, to say NRVO duplicate elimination function under the same file should be the same

CodePudding user response:

Have found that if the if (index<0 | | index & gt;=length | | subLenght<0) return ""; If the line commented out, the result would be as good as overloaded + +
Also in overloaded + + function into such, + + function in return will also copy constitutive function called

String String: : operator++ () {
If (length=0) return "";//add it, no matter how didn't get a return on the final TMP will call again copy constructor is estimated to be return "" cause I don't know why

Char * tempPtr=new char [8].
Strcpy (tempPtr, "123456");
String TMP (tempPtr);
The delete [] tempPtr;
+ + this - & gt; Length;
Return the TMP;
}

CodePudding user response:

refer to 6th floor xiexiejohn response:
//x.c pp: this file contains the "main" function, the program will begin and end here, 
//

#include
#include
# pragma warning (4996) disable:
using namespace std;
The class String {
Friend ostream& Operator<(ostream& , const String&);
Public:
String (const char *="");
String (const String&);
~ the String ();
String operator () (int, int=0) const;
String operator++ (int);
Private:
int length;
Char * sPtr;
Void setString (const char *);
};
Ostream& Operator<(ostream& The output, const String& S) {
The output & lt; Return the output;
}
String: : String (const char * s) : length (s!=0? Strlen (s) : 0) {
//cout & lt; <"Calling the default constructor, general constructor, conversion constructors" & lt; SetString (s);
}
String: : String (const String& nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull