The type of pointer is irrelevant, its always an integer
Example:
int var = 9;
int* ptr = &var; // ptr holds where in memory we are storing var
*ptr = 10; // Access the variable from the pointer
// Now var has a value of 10
Example:
Entity entity; // Value based: Local to this function stack frame
Entity* entity_ptr = &entity;
std::cout << (*entity_ptr).GetField() << std::endl; // Option 1 To get value of pointer
std::cout << entity_ptr->GetField() << std::endl; // Option 2 (Wrapper) To get value of pointer
Pointers are also variables, so we could have pointers of pointers (double pointers)
int var = 9;
int* ptr = &var; // ptr is memory address of var
int** double_ptr = &ptr; // double_ptr is memory address of ptr
Example:
include <memory> // Needs to be included to use smart pointers
{
std::unique_ptr<Entity> entity = std::make_unique<Entity>(); // Gets created
}
// Here entity is automatically destroyed (out of scope)
Example:
include <memory> // Needs to be included to use smart pointers
{
std::shared_ptr<Entity> e0;
{
std::shared_ptr<Entity> entity = std::make_shared<Entity>(); // Gets created (references count = 1)
e0 = entity; // assigned to e0 (references count = 2)
}
// entity out of scope but doesnt get destroyed (references count = 1)
}
// here the memory is freed, all references are dead (references count = 0)
Example:
include <memory> // Needs to be included to use smart pointers
{
std::weak_ptr<Entity> e0;
{
std::shared_ptr<Entity> entity = std::make_shared<Entity>(); // Gets created (references count = 1)
e0 = entity; // assigned to e0 but since its weak: (references count = 1)
}
// entity out of scope gets destroyed (references count = 0)
}