#include #include #include "functions.h" using namespace std; int main() { // general int* pNumber = nullptr; // pointer definition, always initialize a pointer (at least with nullptr) pNumber = new int(0); // creates a new integer value on the heap cout << "size of pointer: " << sizeof(pNumber) << endl; cout << "size of pointer data: " << sizeof(*pNumber) << endl; *pNumber = 789; //pNumber = new int(0); // BAD BAD - memory leak, because we override an existin g pointer address with a new one without saving or deleting the old one // here are 100s lines of code if (pNumber != nullptr) { delete pNumber; pNumber = nullptr; } safeDelete(pNumber); // pointer as references outside local scope int number = 10; changeInteger(number); number = changeIntegerPerReturn(number); changeIntegerPerPointer(&number); changeIntegerPerPointer(pNumber); changeIntegerPerReference(number); // smart pointer // unique_ptr - only one owner exist for the object // shared_ptr - can have multiple owners // weak_ptr - is a loose connection to a shared pointer unique_ptr pNumber2 = make_unique(10); *pNumber2 = 100; //unique_ptr pNumber3 = pNumber2; // compiler error- you can't create a copy of an unique pointer unique_ptr pNumber4 = make_unique(*pNumber2); shared_ptr pNumber5 = make_shared(10); shared_ptr pNumber6 = pNumber5; *pNumber6 = 100; pNumber5.reset(); weak_ptr pNumber7 = pNumber6; //*pNumber7 = 1000; // compiler error, weak pointer has no direct access to a shared pointer, you have to create a new shared pointer if (!pNumber7.expired()) { shared_ptr tmpSharedNumber = pNumber7.lock(); *tmpSharedNumber = 10000; } pNumber6.reset(); shared_ptr tmpSharedNumber2 = pNumber7.lock(); if (tmpSharedNumber2 != nullptr) *tmpSharedNumber2 = 100000; // pointer-pointer createInteger(pNumber); createIntegerWithPointerPointer(&pNumber); if (pNumber != nullptr) { delete pNumber; pNumber = nullptr; } // const example const int constNumber = 10; //constNumber = 20; // a const value can not be changed const int* constNumberPointer = new int(10); //*constNumberPointer = 20; // this is a const value constNumberPointer = new int(20); // but not a const pointer int const* numberConstPointer = new int(10); //*numberConstPointer = 20; // this is a const value numberConstPointer = new int(20); // but not a const pointer int* const numberPointerConst = new int(10); *numberPointerConst = 20; // the value is not const //numberPointerConst = new int(20); // but the pointer is const const int* const constNumberPointerConst = new int(10); //*constNumberPointerConst = 20; // this is a const value //constNumberPointerConst = new int(20); // and the pointer as well int const* const numberConstPointerConst = new int(10); //*numberConstPointerConst = 20; // this is a const value //numberConstPointerConst = new int(20); // and the pointer as well const int const* const constNumberConstPointerConst = new int(10); //*constNumberConstPointerConst = 20; // this is a const value //constNumberConstPointerConst = new int(20); // and the pointer as well return 0; }