In the code that is on the bottom of this post, I have a problem in qnx where the desrtuctor gets called twice when I compile and run it with qnx. However if I compile it and run it in windows with gcc using dev-cpp I get the result that I would expect. Is there a flag or something that I need in qnx or do I have an error somewhere?
compiled with qnx with “g++ complex.c -o complex”:
constructor
constructor
constructor
add
destructor
constructor
add
destructor
equal
destructor
destructor
destructor
destructor
you can see that the destructor gets called twice for classes that are created in the addition operator. Its my understanding that the destructor for these should only be called after everthing in the equation has been computed.
output from windows with dev-cpp:
constructor
constructor
constructor
add
constructor
add
equal
destructor
destructor
destructor
destructor
Here is the code:
#include
using std::cout;
class complex{
public:
complex( double=0, double=0);
~complex();
complex &operator=(const complex &right_operand);
complex operator+(const complex &right_operand) const;
private:
double real;
double imaginary;
};
complex::complex( double re, double img){
cout<<“constructor\n”;
real=re;
imaginary=img;
}
complex::~complex(){
cout<<“destructor\n”;
}
complex &complex::operator=(const complex &right_operand){
cout<<“equal\n”;
real=right_operand.real;
imaginary=right_operand.imaginary;
return *this;
}
complex complex::operator+(const complex &right_operand) const{
complex temp;
cout<<“add\n”;
temp.real=right_operand.real+real;
temp.imaginary=right_operand.imaginary+imaginary;
return temp;
}
main(){
complex A(2,4), B;
B=A+A+A;
}