Brief

Pointers

A pointer is a variable type that store the address (or reference) of another variable type. Declaring a pointer is simple:

int * myPointer;

The code above declares a pointer to an integer. When declaring a pointer, you must follow the sequence Data Type That Will Be Pointed To, *, Name of Pointer Variable. In this case, the data type that will be pointed to is indicated by int, the * follows, and the name of the pointer variable is myPointer.

To assign an address to the pointer, you must use the & reference sign to obtain the address of the variable you want to assign it to, or use the C++ new keyword.

int myVariable;
myPointer = &myVariables;
MyClass * classPointer = new MyClass();

The line myPointer = &myVariables obtains the address of myVariable with the & and assigns it to myPointer.

The line MyClass * classPointer = new MyClass() allocates a new memory space containing a MyClass object, and assigns the address of the object to classPointer.

To dereference a pointer (to access the variable referenced by your pointer) simply put an * next to the pointer.

When working with classes or structs to reference members or methods, you have two options:

  1. (*myPointer).method
  2. myPointer->method
*myPointer = 5;

myClassPointer->member = 5;
(*myClassPointer).method();

Taking a look at the code above, *myPointer = 5 sets the value at the address location myPointer references to 5 (you may have to read that sentence a few times to understand ¯\_(ツ)_/¯ ). myClassPointer->member = 5 sets the class member of the class instance that myClassPointer points to, to 5. (*myClassPointer).method() calls the method of the class instance that myClassPointer points to.

Data manipulation can be done from either a pointer or the variable/object.

*myPointer = 3;
cout << myVariable << endl;
myVariable = 4;
cout << *myPointer << endl;

The line cout << myVariable << endl will print a 3 and the line cout << *myPointer << endl will print a 4.

References

A reference can be thought of as a joint clone to another variable or object and is denoted by &

int aNumber = 10;
int & myReference = aNumber;

The code above created a reference to an int called myReference and initialize it to aNumber. The object or variable that a reference references, can have it’s data directly manipulated without needing to dereference. Note: a reference must be initialized when it is declared, otherwise it will not compile.

int myVariable;
int & myReference = myVariables;

myReference = 3;
cout << myVariable << endl;
myVariable = 4;
cout << myReference << endl;

The line cout << myVariable << endl will print a 3 and the line cout << myReference << endl will print a 4.

Not as commonly used as pointers (are unique to C++).

Passing Arguments By Reference Or By Value

Most simple function calls pass arguments by value. This means that when a function is called, it copies the value of the variable passed in, and never changes the value of the variable passed in itself.

int add(int a)
{
    a = a + 2;
    return a;
};

int myVar = 6;

cout << add(myVar) << endl;
cout << myVar << endl;

The lines cout << add(myVar) << endl and cout << myVar << endl print 8 and 6 respectively. myVar ends up being 6 because it was not passed by reference.

If a function has an argument by reference, it means it takes the address of the variable passed in, and the function now has the ability to manipulate the variable since it knows its memory address’.

int add2(int &a)
{
    a = a + 2;
    return a;
};

int myVar = 6;


cout << add2(myVar) << endl;
cout << myVar << endl;

With the modification of passing by reference, now the lines cout << add(myVar) << endl and cout << myVar << endl print 8.


Improve this page