#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Testi {
public:
string name;
Testi(string a) : name(a) {
cout << "Im alive: " << name << endl;
}
~Testi() {
cout << "Im no longer alive: " << name << endl;
}
};
int main() {
vector <Testi> a;
a.emplace_back(Testi("John"));
a.emplace_back(Testi("Jack"));
a.emplace_back(Testi("Jake"));
cout << a[1].name; // in case to see if the object still exist.
cin.get();
return 0;
}
OUTPUT:
Im alive: John
Im no longer alive: John
Im alive: Jack
Im no longer alive: John
Im no longer alive: Jack
Im alive: Jake
Im no longer alive: John
Im no longer alive: Jack
Im no longer alive: Jake
Jack
No i po inpucie:
Im no longer alive: John
Im no longer alive: Jack
Im no longer alive: Jake
Dlaczego mimo, że używam emplace_back() zamiast push_back() tworzone są tymczasowe obiekty?