#include
#include
// std::unique_ptr, std::shared_ptr, std::weak_ptr.
// smart pointers, when you call new, you don't have to call delete.
// It's a wrapper around a real raw pointer, it'll call new when you make it.
//
// unique_ptr:scoped pointer, it will get destroyed if it goes out of scope. can't be copied.
//
// shared_ptr:working by reference counting, keeps track of how many references you have to your pointer,
// as soon as that reference count reaches 0 that's when it gets deleted. can be copied.
//
// weak_ptr:assign a shared_ptr to another shared_ptr, it will increase the reference count but
// when you assign a shared_ptr to a weak_ptr it won't increase the reference count.
//
// unique_ptr has a lower overhead, but shared_ptr is also useful in some cases, so is weak_ptr.
class Entity
{
public:
Entity(){ std::cout << "Created Entity!" << std::endl; }
~Entity(){ std::cout << "Destroyed Entity!" << std::endl; }
void Function(){}
};
int main(void)
{
{
std::shared_ptr<Entity> e0;
{
//std::unique_ptr entity = std::make_unique();//more preferred way, slightly safer than std::unique_ptr uniqueEntity(new Entity());
//entity->Function();
std::unique_ptr another = entity;//error, can't copy a unique pointer.
std::shared_ptr<Entity> sharedEntity = std::make_shared<Entity>();
//std::shared_ptr sharedEntity(new Entity());//can do this too.
//std::shared_ptr another = sharedEntity;//works.
e0 = sharedEntity;//works.
std::weak_ptr<Entity> weakEntity = sharedEntity;
}
}
std::cin.get();
}