You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: C++ Syntax.md
+38-2Lines changed: 38 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -866,7 +866,7 @@ C++ code to pass an object as a `const` reference (if the data should be unmutab
866
866
867
867
More on [references vs pointers here](https://stackoverflow.com/a/57492).
868
868
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.
870
870
871
871
```c++
872
872
// Pointers
@@ -907,8 +907,44 @@ std::cout << a << std::endl; // ALSO PRINTS: 20 !
907
907
int & ref_c; // ERROR! References must be initialized at their declaration
908
908
```
909
909
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.
911
913
914
+
```c++
915
+
// Pass by reference using a const reference
916
+
voidFoo(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
0 commit comments