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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
int m_value = 1;
void func0(int *p) { *p = m_value; }
void func1(int *p) { p = &m_value; }
void func2(int **p) { *p = &m_value; }
void func3(int *&p) { p = &m_value; }
void func4(int &p) { p = m_value; }
void func5(int p) { p = m_value; }
void print(const std::string &func, const int num1, const int num2) { std::cout << "*****************" << std::endl; std::cout << func << ":" << std::endl; std::cout << "*pn" << ": " << num1 << std::endl; std::cout << "l_value" << ": " << num2 << std::endl; std::cout << "*****************" << std::endl; }
int main(int argv, char **argc) { int l_value = 5; int *pn = &l_value; int &ref = l_value; std::cout << "sizeof(pn): " << sizeof(pn) << std::endl; std::cout << "sizeof(*pn): " << sizeof(*pn) << std::endl; std::cout << "sizeof(ref): " << sizeof(ref) << std::endl;
std::cout << "l_value: " << l_value << std::endl;
func0(pn); print("func0", *pn, l_value); l_value = 5; pn = &l_value;
func1(pn); print("func1", *pn, l_value); l_value = 5; pn = &l_value;
func2(&pn); print("func2", *pn, l_value); std::cout << "func2: " << l_value << std::endl; l_value = 5; pn = &l_value;
func3(pn); print("func3", *pn, l_value); std::cout << "func3: " << l_value << std::endl; l_value = 5; pn = &l_value;
func4(l_value); print("func4", *pn, l_value); l_value = 5;
func5(l_value); print("func5", *pn, l_value);
return 0; }
|