IMPORTANT: the following things are the same:
AActor* MyActor = nullptr; // This is how Unreal is displaying pointers, the asterisk comes after the variable type
AActor *MyActor = nullptr; // This is how it's mostly done in regular C++ and shown in tutorials, the asterisk is in front of the actual variable (name)
A few helpful examples to get a quick understanding:
int Number {42}; // a regular number
int* NumberPtr; // a pointer of type int, does not have a valid address and will therefore return garbage address
int *NumberPtr = nullptr; // correct initialization, now it will return a 0 adress
int* const NewPtr {&Number}; // Constant pointer. Variable "Number" can be changed, but not the pointer itself.
const int* NewPtr {&Number}; // Constant integer value. The pointer can change the address, but the "Number" it points to can't be changed anymore.
Hint: especially for constants, make sure to read from right to left.
NumberPtr = &Number; // Will assigne the address (& address reference) if "Number" to "NumberPtr"
cout << Number; // will return "42"
cout << NumberPtr; // will return the address of "Number", NOT the value
cout << *NumberPtr; // de-referenced pointer, will return "42" since "Number" equals 42
*NumberPtr = 69; // this will access the address of "Number" and change its value, since the * at the beginning de-references the pointer
cout << *NumberPtr; // this will now display "69"
Number = 666; // now we change the number value itself
cout << *NumberPtr; // this will now display "666", because it points to the adress of "Number", where the value was just changed to "666"
Weirdly enough, pointers are both declared with an * in front and also de-referenced by it.
int* Pointer; // initialization with garbage address
int* Pointer {nullptr}; // correct initialization with address of zero (cout << Pointer will return 0)
int* Pointer {&Variable}; // if Variable is an int and was previously defined, Pointer will now point to its address
Pointers can contain a nullptr/NULL value
Pointers are usually used for AActors and UObjects within Unreal
A few helpful examples to get a quick understanding:
int Number {42}; // a regular number
int& NumberReference = Number; // an adress to an integer, in this case: Number (above)
cout << Number; // will print 42
cout << NumberReference; // will also print 42
Iterationg through a reference:
vector<string> Names {"Noah", "Mio", "Eunie"};
for (auto const &Item: Names) // iterate through the vector, use a reference to "Names" -> const makes sure, the original "Names" vector (its values) can't be changed
{
cout << Item << endl; // print the Name as a string, "Item" can also be "i", "CurrentName" etc -> the currently looped through name of the Names vector array
}
References can NOT contain a nullptr/NULL value
References are usually used for Functions (UFunctions) within Unreal
New is allocating new memory during runtime.
int32_t* MyArray = new int32_t[4];
Once used, we can free the memory.
delete [] MyArray;
MyArray = nullptr;
This is an easy example on how to allocate and de-allocate memory to create a dynamic array in C++.
The last entry, setting MyArray to a nullptr is helpful because: if trying to access it later will lead to the old address that may contain other data.
A good tutorial can be found HERE.
Another example using a simple variable type:
New is allocating new memory during runtime.
int32_t* MyInt32 = new int32_t;
*MyInt32 = 42;
Once used, we can free the memory.
delete MyInt32;
MyInt32 = nullptr;
Variant 1: Copy Initialization,
copy the assignment of 12 into the variable MyInt.
int32_t MyInt = 12;
Variant 2: List initialization,
directly initializing 12 into MyInt.
int32_t MyInt {12};
Variant 3: Copy list initialization,
copy the list initialization of 12 into MyInt.
int32_t MyInt = {12};
Variant 4: Value initialization,
first initialize value to empty and then copy 12 into the variable MyInt.
int32_t MyInt {};
MyInt = 12;
Variant 5: Direct initialization,
directly initialize 12 into the variable MyInt.
int32_t MyInt (12);