Brief
Pointers
A pointer is a variable type that store the address (or reference) of another variable type. Declaring a pointer is simple:
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.
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:
(*myPointer).method
myPointer->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.
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 &
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.
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.
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’.
With the modification of passing by reference, now the lines cout << add(myVar) << endl
and cout << myVar << endl
print 8.
- Previous
- Next