M
michael
Hi All,
I have written the following to illustrate a problem.
I know I have some magic numbers etc please ignore them.
What I do not follow is why the line marked results in a call to the
destructor for the object.
Can someone please explain it for me?
#include <iostream>
#include <fstream>
using std:stream;
class someClass {
private:
char *str;
public:
someClass();
~someClass();
friend ostream& operator <<(ostream& lhs, someClass rhs);
};
someClass::someClass(){
str = new char[10];
strcpy(str, "something");
}
someClass::~someClass(){
std::cout << "\nIn someClass destructor...\n";
delete str;
}
ostream& operator <<(ostream& lhs, someClass rhs){
lhs << rhs.str; // This results in a call to the destructor for
the someClass object....why?
return lhs;
}
int main(){
someClass soc;
std::cout << soc;
}
Thanks for your help
Regards
Michael
I have written the following to illustrate a problem.
I know I have some magic numbers etc please ignore them.
What I do not follow is why the line marked results in a call to the
destructor for the object.
Can someone please explain it for me?
#include <iostream>
#include <fstream>
using std:stream;
class someClass {
private:
char *str;
public:
someClass();
~someClass();
friend ostream& operator <<(ostream& lhs, someClass rhs);
};
someClass::someClass(){
str = new char[10];
strcpy(str, "something");
}
someClass::~someClass(){
std::cout << "\nIn someClass destructor...\n";
delete str;
}
ostream& operator <<(ostream& lhs, someClass rhs){
lhs << rhs.str; // This results in a call to the destructor for
the someClass object....why?
return lhs;
}
int main(){
someClass soc;
std::cout << soc;
}
Thanks for your help
Regards
Michael