unique_ptr et autres types de pointeur c++ code example

Example 1: unique_ptr in c++

#include <iostream>
class Entity {
public:
	int b = 0;
	Entity() { std::cout << "[CREATED ENTITY]" << std::endl; };
	~Entity() { std::cout << "[Destroyed ENTITY]" << std::endl; };
	void Print() {};
};
int main()
{

	{
		std::unique_ptr<Entity> entity = std::make_unique<Entity>();
		//std::unique_ptr<Entity> entity1 = entity;//can't copy
		
		entity->Print();
		
	}

}

Example 2: c++ get pointer from unique_ptr

#include <memory>

std::unique_ptr<int> myUniquePtr = std::unique_optr<int>(new int(5));
int* rawPtr = myUniquePtr.get();

Tags:

Cpp Example