Skip to content

Commit 3cc3dd5

Browse files
committed
Refactor references/pointer section
1 parent eee0ed4 commit 3cc3dd5

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

C++ Syntax.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ C++ code to pass an object as a `const` reference (if the data should be unmutab
866866

867867
More on [references vs pointers here](https://stackoverflow.com/a/57492).
868868

869-
In the following code, assume a 32-bit system, in which case the size of a pointer variable is 4 bytes (32 bits), and that the stack grows towards higher memory addresses.
869+
In the following code, assume a 32-bit system, in which case the size of a pointer variable is 4 bytes, and that the stack grows towards higher memory addresses.
870870

871871
```c++
872872
// Pointers
@@ -907,8 +907,44 @@ std::cout << a << std::endl; // ALSO PRINTS: 20 !
907907
int & ref_c; // ERROR! References must be initialized at their declaration
908908
```
909909

910-
In many cases
910+
Perhaps the most widely used aspect of references is to pass objects by reference (sometimes constant reference) to a method. To avoid hammering the stack with
911+
large objects when you pass them by value it is nearly always preferrable to pass by reference, which is the term used when using either a reference *or* a pointer.
912+
Using a reference allows you to pass any size object by reference, while still allowing you to access the object directly.
911913

914+
```c++
915+
// Pass by reference using a const reference
916+
void Foo(const Bar & bar) {
917+
int a = bar.GetValue();
918+
919+
if (bar.SomeMethod()) {
920+
// ...
921+
}
922+
923+
bar.SetValue(10); // ERROR! Cannot modify a const reference!
924+
}
925+
926+
// Pass by reference using a non-const reference
927+
void Foo(Bar & bar) {
928+
int a = bar.GetValue();
929+
930+
if (bar.SomeMethod()) {
931+
// ...
932+
}
933+
934+
bar.SetValue(10); // Modifies 'bar' and thus whatever 'bar' references
935+
}
936+
```
937+
938+
By passing an object by reference using a reference instead of a pointer you:
939+
* Don't need to check for `NULL` or `nullptr` since references cannot be null
940+
* Can access the referenced object's data directly instead of using the `->` operator or dereferencing a pointer
941+
* Make it clearer which parameters are meant to be *input* parameters and which are meant to be *output* parameters by using
942+
`const` to denote strictly input parameters
943+
* Gain the benefits of both passing by value and passing by reference since you don't need to use a lot of memory on the stack for your object
944+
945+
Thus, passing by reference using a `const` reference is essentially the same as passing by value, but you avoid copying the object onto the stack. Passing by reference
946+
using a non-const reference is essentially the same as passing by reference using a pointer, but you are guaranteed that it is not null and it's as if the pointer
947+
is effectively dereferenced.
912948
913949
### 2.3 Keywords
914950
[Reference](http://en.cppreference.com/w/cpp/keyword)

0 commit comments

Comments
 (0)