ANSI C++ in qnx

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;

}

Without trying to figure out why, I tried compiling it like this:
QCC -o testcc testcc.cc
and got the same results as you. When I tried using the 3.3.1 version of the compiler, like this:
QCC -V3.3.1,gcc_ntox86 -o testcc testcc.cc
I got the same results as you did in windows.

My guess is the default (2.95.3) version of the compiler has a bug and it is fixed in 3.3.1.

Rick…

hmmm … another 2.95.3 issue … interresting …