With following source files:
Obj.h
class Obj
{
public:
Obj();
Obj(const Obj &other);
~Obj();
};
Obj.cpp
#include <iostream>
#include "Obj.h"
using namespace std;
Obj::Obj(){ cout << "create "; }
Obj::Obj(const Obj &other){ cout << "copy "; }
Obj::~Obj(){ cout << "delete "; }
main.cpp
#include <vector>
#include "Obj.h"
using namespace std;
vector<Obj> objects;
int main(int argc, char** argv)
{
for (int i = 0; i < 4; i++)
objects.push_back(*(new Obj));
//system("pause");
}
The program outputs: create copy create copy copy delete create copy copy copy delete delete create copy copy copy copy delete delete delete
Why can't it be just 'create, create, create, create'?
I'm out of ideas how to fix that problem and it doesn't help that I've just started learning pointers and the c++ itself. Any help appreciated!
I'm using Visual Studio 2017 with whatever compiler there's by default.