Poniżej przykład:
//---------------------------------------------------------------------------
#include <vcl.h>
#include "test.h"
#include <iostream>
#include <vector.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
using namespace std;
int main(int argc, char* argv[])
{
vector<test> g;
// test t1, t2;
// g.push_back(test(3));
g.reserve(10);
g.push_back(test(4));
g.push_back(test());
cout<<"liczba ob: "<<g.size()<<endl;
g.pop_back();
getchar();
return 0;
}
//---------------------------------------------------------------------------
i plik test.h:
#include <iostream>
using namespace std;
class test
{
private:
int x;
public:
test()
{
x=5;
cout<<"const"<<endl;
}
test(int i)
{
x=i;
cout<<"const i"<<endl;
}
~test()
{
cout<<"dest"<<endl;
}
};
a oto rezultat:
const i
dest
const
dest
liczba ob: 2
dest
Dlaczego tak się dzieje?