M
MirzaD
Hello
Below you will find the problematic program. It is a string wrapper
class with a bare minimum of functionality to keep things simple.
**Code**
//StringClass.h
#include <iostream>
#include <cstring>
using namespace std;
class StringClass{
char* s;
int size;
public:
char* name;
StringClass();
StringClass(char* string);
~StringClass();
StringClass operator=(StringClass &string);
friend ostream &operator<<(ostream &output, StringClass &string);
};
StringClass::StringClass(){
s = new char[0];
size = 0;
}
StringClass::StringClass(char* string){
size = strlen(string);
s = new char[size + 1];
strcpy(s,string);
}
StringClass::~StringClass(){
cout << "Destructor: " << name << endl;
delete [] s;
}
StringClass StringClass:
perator=(StringClass &string){
char* temp;
try{
temp = new char[string.size + 1];
}
catch (bad_alloc ex){
exit(1);
}
strcpy(temp,string.s);
delete [] s;
s = temp;
return *this;
}
ostream &operator<<(ostream &out, StringClass &string){
out << string.s;
return out;
}
//StringClass.cpp
#include "StringClass.h"
using namespace std;
int main(){
StringClass s("test"),t;
s.name = "s";
t.name = "t";
t = s;
cout << "s = " << s << endl << "t = " << t <<endl;
return 0;
}
**End code**
The above will display the following:
Destructor: t
s = test;
t = $*%
Destructor: t
Destructor: s
Why is the destructor called on t after the assignment takes place?
This would make sense to me if t was a pointer and the destructor was
called on the object it was pointing to (the last reference to the
object was just removed). Any help/clarification is appreciated.
Regards,
Mirza
Below you will find the problematic program. It is a string wrapper
class with a bare minimum of functionality to keep things simple.
**Code**
//StringClass.h
#include <iostream>
#include <cstring>
using namespace std;
class StringClass{
char* s;
int size;
public:
char* name;
StringClass();
StringClass(char* string);
~StringClass();
StringClass operator=(StringClass &string);
friend ostream &operator<<(ostream &output, StringClass &string);
};
StringClass::StringClass(){
s = new char[0];
size = 0;
}
StringClass::StringClass(char* string){
size = strlen(string);
s = new char[size + 1];
strcpy(s,string);
}
StringClass::~StringClass(){
cout << "Destructor: " << name << endl;
delete [] s;
}
StringClass StringClass:
char* temp;
try{
temp = new char[string.size + 1];
}
catch (bad_alloc ex){
exit(1);
}
strcpy(temp,string.s);
delete [] s;
s = temp;
return *this;
}
ostream &operator<<(ostream &out, StringClass &string){
out << string.s;
return out;
}
//StringClass.cpp
#include "StringClass.h"
using namespace std;
int main(){
StringClass s("test"),t;
s.name = "s";
t.name = "t";
t = s;
cout << "s = " << s << endl << "t = " << t <<endl;
return 0;
}
**End code**
The above will display the following:
Destructor: t
s = test;
t = $*%
Destructor: t
Destructor: s
Why is the destructor called on t after the assignment takes place?
This would make sense to me if t was a pointer and the destructor was
called on the object it was pointing to (the last reference to the
object was just removed). Any help/clarification is appreciated.
Regards,
Mirza