On-Machine tutoring. A student's program is inexplicable. She was doing a string class, primarily to implement a string connection.
The program is like this, ask the reader to copy it to the IDE, while watching the edge tune:
#include <iostream>#include <cassert>#include <cstring>using namespace STD;classstring{Private:Char* p;intLen Public: String (); String (Const Char* s); String (Conststring& s); ~string ();//The problem appears here voidShow ();friendStringoperator+(Conststring& S1,Conststring& S2);friendStringoperator-(Conststring& S1,Conststring& s2);}; String::string () {len=0; P=null;} String::string (Const Char* s) {len=strlen(s); p=New Char[len+1];strcpy(p,s);} String::string (Conststring& s) {Len=s.len;if(P!=null)Delete[]p; p=New Char[len+1];strcpy(P,S.P);} Stringoperator+(Conststring& S1,Conststring& S2) {String total; Total.len=s1.len+s2.len; total.p=New Char[total.len+1];strcpy(TOTAL.P,S1.P);strcat(TOTAL.P,S2.P);returnTotal;} Stringoperator-(Conststring& S1,Conststring& S2) {Char* c1=New Char[s1.len+1];strcpy(C1,S1.P);inti=s1.len-1; while(s1.p[i]!="'&&i>=0)-I.; c1[i+1]=' + ';Char* c2=New Char[s2.len+1];strcpy(C2,S2.P); I=0; while(i<s2.len&&c2[i]=="') ++i;intj=0; while(c2[i]!=' + '&&i<s2.len) {C2[j]=c2[i]; ++i; ++j; } c2[j]=' + '; String s; S.len=s1.len+s2.len; s.p=New Char[S.len];strcpy(S.P,C1);strcat(S.P,C2);DeleteC1;DeleteC2;returns;}voidString::show () {cout<<p<<endl;} String::~string () {Delete[]p;}intMain () {String str1 ("123"), STR2 ("456"), STR3; STR3=STR1+STR2; Str3.show (); STR3=STR1-STR2; Str3.show ();return 0;}
The performance of the problem is that str3.show(); the output is a few inexplicable symbols!
I asked her to follow it one step at a time, but found that no matter where the breakpoint was located, it was always going into the destructor ~string ()!
Just the other students have a lot of questions, I asked her to send me this program email, she does not entangled in this. I understand that there are pointer members here. Jailbait should be related to this.
Learning more and more in-depth, students problems of the procedure, often need to read carefully before giving guidance.
After a nap, open the message. By copying the program into the IDE, you have noticed an str3=str1+str2; overload of the assignment operator = that is required for execution. Try to find a single step when entering the ~string () when the scene, but attempted.
Under my IDE, str3=str1+str2; the results are normal and str3=str1-str2; the results are abnormal. The characterization of the wild hands, the most headache mixed authenticity.
Overloaded assignment operation =, in the class declaration add:
String &operator=(constString& s1);
Implemented as:
String &String::operator=(constString &s1){ if(!this->p) delete []p; p=new char(s1.len+1); strcpy(p,s1.p); len=s1.len; return *this;}
Hand in the homework to the students first. Does the question of entering ~string () still be? Put it down for the time being.
An issue with operator overloading in a string class