Z
Zachary Turner
#include <vector>
#include <iostream>
int g_unique = 0;
class TestClass
{
private:
int _i;
int _u;
public:
TestClass(int i) : _i(i), _u(g_unique++) { std::cout <<
"Constructor " << _i << ", " << _u << std::endl; }
TestClass(const TestClass& rhs) : _i(rhs._i), _u(g_unique++)
{ std::cout << "Copy constructor " << _i << ", " << _u << std::endl; }
~TestClass() { std::cout << "Destructor " << _i << ", " << _u <<
std::endl; }
};
int main(int argc, char** argv)
{
std::vector<TestClass> test;
std::cout << "Pushing back item 1" << std::endl;
test.push_back(TestClass(1));
std::cout << "Pushing back item 2" << std::endl;
test.push_back(TestClass(2));
std::cout << "Erasing item 1" << std::endl;
test.erase(test.begin());
return 0;
}
Output:
Pushing back item 1
Constructor 1, 0
Copy constructor 1, 1
Copy constructor 1, 2
Destructor 1, 1
Destructor 1, 0
Pushing back item 2
Constructor 2, 3
Copy constructor 2, 4
Copy constructor 1, 5
Copy constructor 2, 6
Destructor 1, 2
Destructor 2, 4
Destructor 2, 3
Erasing item 1
Destructor 2, 6
Destructor 2, 6
As we can see from this the following items get constructed:
1, 0
1, 1
1, 2
2, 3
2, 4
1, 5
2, 6
Makes sense so far. The global unique variable shows us that over the
life of the program, 7 unique items get created. I would expect a
matching destructor for every one of these, but instead we see that
the destructor sequence is as follows:
1, 1
1, 0
1, 2
2, 4
2, 3
2, 6
2, 6
2, 6 gets destroyed twice, and 1, 5 NEVER gets destroyed
Is this correct behavior, and if so why?
#include <iostream>
int g_unique = 0;
class TestClass
{
private:
int _i;
int _u;
public:
TestClass(int i) : _i(i), _u(g_unique++) { std::cout <<
"Constructor " << _i << ", " << _u << std::endl; }
TestClass(const TestClass& rhs) : _i(rhs._i), _u(g_unique++)
{ std::cout << "Copy constructor " << _i << ", " << _u << std::endl; }
~TestClass() { std::cout << "Destructor " << _i << ", " << _u <<
std::endl; }
};
int main(int argc, char** argv)
{
std::vector<TestClass> test;
std::cout << "Pushing back item 1" << std::endl;
test.push_back(TestClass(1));
std::cout << "Pushing back item 2" << std::endl;
test.push_back(TestClass(2));
std::cout << "Erasing item 1" << std::endl;
test.erase(test.begin());
return 0;
}
Output:
Pushing back item 1
Constructor 1, 0
Copy constructor 1, 1
Copy constructor 1, 2
Destructor 1, 1
Destructor 1, 0
Pushing back item 2
Constructor 2, 3
Copy constructor 2, 4
Copy constructor 1, 5
Copy constructor 2, 6
Destructor 1, 2
Destructor 2, 4
Destructor 2, 3
Erasing item 1
Destructor 2, 6
Destructor 2, 6
As we can see from this the following items get constructed:
1, 0
1, 1
1, 2
2, 3
2, 4
1, 5
2, 6
Makes sense so far. The global unique variable shows us that over the
life of the program, 7 unique items get created. I would expect a
matching destructor for every one of these, but instead we see that
the destructor sequence is as follows:
1, 1
1, 0
1, 2
2, 4
2, 3
2, 6
2, 6
2, 6 gets destroyed twice, and 1, 5 NEVER gets destroyed
Is this correct behavior, and if so why?