Hello,
Does anyone have experience with Destructors causing problems?
I am trying to make a String class and when i call methods accepting a String object, the program bomb. (VisC++6 bombs but DevC++ Bloodshed is ok).
If i comment out delete etc in the Destructor, then all is well.
In main() i declare 2 string objects:
MyString s1 = "Big DOG;
MyString s2 = "big dog" ;
cout << s1.Compare(s2); method shown below / / Sometimes ok but,
if i do
S2.getValue(); // returns char* to s2 string.
this bombs, but s1.getValue() is fine.
Any input very much appreciated, thanks.
******
int MyString::Compare(MyString S){
char *cptr = 0;
cptr = this->cpString;
int iSLength = 0;
int iLength = 0;
iSLength = S.GetLength();
iLength = this->GetLength();
if( iLength != iSLength){
return 0;
}
else{
while(*cptr){
if(*cptr != *(S.cpString)){
return 0;
}
cptr++;
S.cpString++;
}
return 1;
}
}
// Destructor
MyString::~MyString(){
delete [] cpString;
cpString = 0;
}