G
gw7rib
I was doing a bit of experimenting prior to writing an overloaded
operator, to see when a temporary was destroyed. I'm a bit unhappy at
the result. This is my code (it's supposed to be a normal overloaded +
and =, but with the actual calculation omitted and names used for
tracking. And yes, it's a bit C-like):
#include <stdio.h>
#include <string.h>
class Thing {
char name[20];
public:
Thing(char *namein); // normal constructor
Thing(const Thing& other); // copy constuctor
~Thing(void);
Thing operator+(const Thing& in);
Thing& operator=(const Thing& in);
};
Thing::Thing(char *namein) {
strcpy(name, namein);
printf("Constructing %s\n", name);
}
Thing::Thing(const Thing& other) { // copy constuctor
strcpy(name, other.name);
strcat(name, "c");
printf("Copy constructing %s\n", name);
}
Thing::~Thing(void) {
printf("Destroying %s\n", name);
}
Thing Thing:
perator+(const Thing& in) {
printf("Beginning add\n");
Thing result("R");
printf("About to end add\n");
return result;
}
Thing& Thing:
perator=(const Thing& in) {
printf("Doing assignment\n");
return *this;
}
int main(void) {
Thing a("A"), b("B"), c("C");
c = a + b;
printf("Finished\n");
}
and this is the result:
Constructing A
Constructing B
Constructing C
Beginning add
Constructing R
About to end add
Copy constructing Rc
Destroying R
Doing assignment
Finished
Destroying Rc
Destroying C
Destroying B
Destroying A
I was expecting that the temporary (Rc) would be destroyed when the
assignment was finished, and I'm surprised that it is apparently still
around after "Finished" is printed. Will it hang around until my
program terminates or is there some way to get rid of it earlier?
Thanks for any advice.
Paul.
operator, to see when a temporary was destroyed. I'm a bit unhappy at
the result. This is my code (it's supposed to be a normal overloaded +
and =, but with the actual calculation omitted and names used for
tracking. And yes, it's a bit C-like):
#include <stdio.h>
#include <string.h>
class Thing {
char name[20];
public:
Thing(char *namein); // normal constructor
Thing(const Thing& other); // copy constuctor
~Thing(void);
Thing operator+(const Thing& in);
Thing& operator=(const Thing& in);
};
Thing::Thing(char *namein) {
strcpy(name, namein);
printf("Constructing %s\n", name);
}
Thing::Thing(const Thing& other) { // copy constuctor
strcpy(name, other.name);
strcat(name, "c");
printf("Copy constructing %s\n", name);
}
Thing::~Thing(void) {
printf("Destroying %s\n", name);
}
Thing Thing:
printf("Beginning add\n");
Thing result("R");
printf("About to end add\n");
return result;
}
Thing& Thing:
printf("Doing assignment\n");
return *this;
}
int main(void) {
Thing a("A"), b("B"), c("C");
c = a + b;
printf("Finished\n");
}
and this is the result:
Constructing A
Constructing B
Constructing C
Beginning add
Constructing R
About to end add
Copy constructing Rc
Destroying R
Doing assignment
Finished
Destroying Rc
Destroying C
Destroying B
Destroying A
I was expecting that the temporary (Rc) would be destroyed when the
assignment was finished, and I'm surprised that it is apparently still
around after "Finished" is printed. Will it hang around until my
program terminates or is there some way to get rid of it earlier?
Thanks for any advice.
Paul.