I tried using push_back and now I get 1 construction and 50 destructions
. The code is below.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct test
{
string String;
int * Integer;
// Constructor
test::test()
{
cout << "Construction!n";
}
// Destructor
test::~test()
{
cout << "Destruction!n";
}
}Stuff;
void main()
{
vector<test> Vector;
for(int n = 0;n<50;n++)
{
Stuff.String = "Some text...";
Vector.push_back(Stuff);
}
int Blank;
cin >> Blank;
}
The reason why I care about the constructions and destructions is that I want to be able to allocate memory dynamically during the construction and deallocate memory during the destruction. Here is some code which is an example of what I want to do (unsurprisingly it does not work):
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct test
{
int * Integer;
// Constructor
test::test()
{
cout << "Construction!n";
Integer = new int;
}
// Destructor
test::~test()
{
cout << "Destruction!n";
delete Integer;
}
};
void main()
{
vector<test> Vector;
Vector.resize(50);
for(int n = 0;n<50;n++)
{
*Vector[n].Integer = n;
}
for(int n = 0;n<50;n++)
{
cout << *Vector[n].Integer << ".n";
}
int Blank;
cin >> Blank;
}