C++ Pointers & Reference

1 minute read

  • Pointer is a variable that holds the address of another variable

    • Pointer can be used to get to the original place that it was pointing to.
    • Change the value that it points to.
    • Pointer can be changed to point to something else.
    • Pointer has it’s own memory address
    • Pointer can be assigned NULL directly with nullptr
    int A;
    int* pA = &A;
    int *pA = &A;
    //to get throught the pointer to its target
    *pA = 5;
    int *pN = NULL;

  • Pointer can point to object
    Person p1;
    Person* pA = &p1;
    // Two posible way to use pointer with object


  • Reference refer to original address of referee
  • Reference should be assigned on initialization
  • Reference take up space in the stack
  • There are no such thing as Null reference it always need to be initialized
    int A = 5;
    int &rA = A;
    int &rN = NULL //Compile error


  • A way to commit to the compiler that value would not be changed
    • When declaring a local value const int zero = 0;
    • As a function parameter int foo(const int i)

    • When we want not to copy the object because the copy is “expensive” we pass it by reference but if we want to prevent some code to change it we add const int something(const Person& p) I give you reference to spare the copy but don’t give you right to change it

    • Modifier on member function int GetName() const; I state that this function does not change any of the member variable of this Class

Const with Pointers

  • Pointer can be const int* const cpX -then you can not change it to point to something else

  • OR pointer can point to something that is const const int* cpX

    • then the pointer can’t be used to change what it points to