01 Pointers
| C | C++ | |
|---|---|---|
| Pointers | Also called “raw pointers” Type: Type*Address-Of operator: &varDereference: *ptrAccess instance method: ptr->...used in combination with new ... and delete ptr | std::unique_ptr (std::make_unique)std::shared_ptr (std::make_shared)std::weak_ptr |
| References | Type: Type&Usage: Type& ref = varAccess instance method: ref. ...References cannot be reassigned and must first be initialized. |
Pointers
Pointers store the address of an object (or function) in memory. They are used to access the object at the address they are pointing to.
A special pointer is the nullptr pointer, which is a pointer that does not point to any object.
Raw pointers are rarely used in modern C++ and should be avoided if possible.
Smart Pointers
Smart pointers are a modern C++ feature that automatically manage the lifetime of an object. They are used to avoid memory leaks and dangling pointers.
Unique Pointers
Unique pointers are used to manage the lifetime of an object that is only referenced by one pointer. The object is destroyed when the pointer goes out of scope.
Shared Pointers
Shared pointers are used to manage the lifetime of an object that is referenced by multiple pointers. A count of how many references exist is stored and the object is destroyed when the count reaches zero.
Weak Pointers
Weak pointers are used to manage the lifetime of an object that is referenced by multiple pointers.
They are used to break reference cycles (and thus generally behave like shared_ptrs).
Pointer Arithmatic
General concept: Calculate new addresses from existing ones. Allowed operations: Increment, decrement, addition, subtraction.
+1 on a pointer of type T will increase the address by sizeof(T).
References
References are only available in C++. They are used as an alias to an existing variable. The reference could be replaced with the variable it is referencing to and the code would still work.
Parameter Passing
Parameters can have be used in different ways and behave differently depending on the type of the parameter:
- They can be used to pass data into a function or to return data from a function.
- They can be passed by value or by reference.
In-Parameter
In-parameters are used to pass data into a function. In-parameters should be marked const to prevent the function from changing the value of the parameter.
void foo(const Point& p) {
int x = p.x;
}
Out-Parameter
Out-parameters are used to return data from a function. Out-parameters should be marked with a pointer or reference to allow the function to change the value of the parameter.
void foo(Point& p) {
p.x = 42;
}
In-Out-Parameter
In-out-parameters are used to pass data into a function and to return data from a function. In-out-parameters should be marked with a pointer or reference to allow the function to change the value of the parameter.
void foo(Point* p) {
p.x = 42;
}
Pass by Value
Pass by value is the default way of passing parameters into a function. The function receives a copy of the parameter and cannot change the value of the original parameter. All operations are done on the copy. It is automatically destroyed when the function returns.
Pass by Reference
Pass by reference is used to pass a parameter into a function without copying it. The caller and the function share the same object. This means changes made by the function are visible to the caller.