Originally Posted by
TheHimself
Thanks. I wrote a destructor for the class and put a print command in it. For a short program which simply declares 3 objects a,b,c and then
the destructor was called 4 times. I think once for each of a,b,c and once for the result of a+b. Interesting!
That's strange...
it only gets called 3 times for me
Code:
$ g++ destruct.cpp
$ ./a.out
mystruct destroyed with i=3
mystruct destroyed with i=2
mystruct destroyed with i=1
is the output from this:
PHP Code:
// g++ destruct.cpp
#include <cstdio>
struct my_struct {
int i;
// no implicit type casts
explicit my_struct(int ii): i(ii) {}
// log destructions
~my_struct() {
printf("mystruct destroyed with i=%d\n", i);
}
private:
// invalidite copying
my_struct & operator= (const my_struct &);
// my_struct(const my_struct &);
};
my_struct operator+ (const my_struct &a, const my_struct &b) {
return my_struct(a.i + b.i);
}
int main() {
my_struct a(1), b(2);
my_struct c = a+b;
return 0;
}
Bookmarks