-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreferencePointerComparison.cpp
More file actions
43 lines (36 loc) · 1.57 KB
/
referencePointerComparison.cpp
File metadata and controls
43 lines (36 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
//inline function declaration adding reference "a" and pointer "b"
inline void add(int& a, int* b) {
std::cout << "Total: " << (a + *b) << std::endl;
}
int main() {
//reference and pointers pass data by reference which is more efficient and encouraged
//program requirements determine if pointer or reference is better to use
//generally, C++ programmers prefer reference over pointer
/* RULE FOR REFERENCE AND POINTER:
* -reference is more efficient than pointer
* -reference is easier to use
* -reference is more convenient to use
* -reference is more flexible than pointer
* -reference is more secure than pointer
* Can be declared without initialization: References(no) Pointers(yes)
* Can be reassigned: References(no) Pointers(yes)
* Can contain a 0(null) value: References(no) Pointers(yes)
*
* USE A REFERENCE UNLESS:
* 1. If you don't want to initialize in the declaration, use a pointer
* 2. If you want to reassign the value, use a pointer
*
*/
int num = 100;
int sum = 500;
int& rNum = num; //initialize reference variable with num
int* ptr = # //initialize pointer variable with num
void (*fn) (int& a, int* b) = add;
std::cout << "Reference: " << rNum << std::endl;
std::cout << "Pointer: " << *ptr << std::endl;
ptr = ∑ //reassign pointer variable to sum
std::cout << "Pointer now: " << *ptr << std::endl;
fn(rNum, ptr); //call function with reference and pointer
return 0;
}