Cheatography
https://cheatography.com
C++ references and pointers in function calls - passing as argument
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Regular variable
// variable definition
int count = 10;
// function definitions
void example( int i) {
cout << i;
i = 30;
}
int main() {
example(count);
cout << count;
return 0;
}
// terminal output
10
10
|
|
|
Reference
// variable definition
int count = 10;
// functions definition
void example( int& i) {
cout << i;
i = 30;
}
int main() {
example(count);
cout << count;
return 0;
}
// terminal output
10
30
|
|
|
Pointer
// variable definition
int count = 10;
// pointer definition
int* pCount = &count;
//functions definition
void example( int* i) {
cout << *i;
*i = 30;
}
int main() {
example(pCount);
cout << count;
return 0;
}
// terminal output
10
30
|
|