My question is: is the following code about STL stack correct?
in the code, complex is a user defined class with constructor and destructor defined. after place 1, complex constructor and destructor are called 5 times respectively, and after place 2, complex destructor are called 5 times again due to pop(). so in total destructor are called more than constructor. IMO it should not happen. Is my code correct? if not correct how to correct it? suppose I still use stack rather than stack
#include <stack>
#include "complex.h"
using namespace std;
void test_stack(){
stack<complex> mystack2;
cout << "Pushing complex..." << endl;
for (int i=0; i<5; ++i) {
complex c(i,i);
mystack2.push(c);
}
//place 1
cout << "Popping out complex..." << endl;
while (!mystack2.empty())
{
cout << " " << mystack2.top();
mystack2.pop(); //void pop();
}
//place 2
cout << endl;
}