0x01 meaning of reference
- References exist as variable aliases, so they can replace pointers in some situations
- References are more readable and practical for pointers
0x02 special reference
1. Const reference
You can declare const references in C ++, and their usage is as follows:
const Type& nmae = var;
The const reference allows the variable to have a read-only attribute instead of the const evolution attribute in C ++.
Code example:
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 4;
const int& b = a;
int* p = (int*)&b;
*p = 5;
printf("a = %d, b = %d\n",a, b);
return 0;
}
operation result:
a = 5, b = 5
请按任意键继续. . .
When using const to modify the reference, C ++ no longer takes the value from the symbol table, at this time the const reference becomes a read-only variable.
2. Constant initialized const reference
When a constant is used to initialize a const reference, the c ++ compiler will allocate space for the constant value and use the reference name as an alias for this space. So in simple terms, using a constant to initialize a const reference will generate a read-only variable.
Sample code:
void Demo()
{
printf("Demo:\n");
const int& c = 1;
int* p = (int*)&c;
//c = 5;
*p = 5;
printf("c = %d\n", c);
}
int main(int argc, char *argv[])
{
Demo();
return 0;
}
operation result:
Demo:
c = 5
请按任意键继续. . .
0x03 Essence of reference
The implementation of the reference inside c ++ is a pointer constant
Type& name ; <==> Type* const name
The c ++ compiler uses the pointer constant as the internal implementation of the reference during compilation, so the size of the space occupied by the reference is the same as the pointer.
From a usage perspective, a reference is just an alias.
Sample code:
#include <stdio.h>
struct TRef
{
char* before;
char& ref;
char* after;
};
int main(int argc, char* argv[])
{
char a = 'a';
char& b = a;
char c = 'c';
TRef r = {&a, b, &c};
printf("sizeof(r) = %d\n", sizeof(r));
printf("sizeof(r.before) = %d\n", sizeof(r.before));
printf("sizeof(r.after) = %d\n", sizeof(r.after));
printf("&r.before = %p\n", &r.before);
printf("&r.after = %p\n", &r.after);
return 0;
}
operation result:
sizeof(r) = 12
sizeof(r.before) = 4
sizeof(r.after) = 4
&r.before = 00DAF938
&r.after = 00DAF940
请按任意键继续. . .
The function returns reference sample code:
#include <stdio.h>
int& demo()
{
int d = 0;
printf("demo: d = %d\n", d);
return d;
}
int& func()
{
static int s = 0;
printf("func: s = %d\n", s);
return s;
}
int main(int argc, char* argv[])
{
int& rd = demo();
int& rs = func();
printf("\n");
printf("main: rd = %d\n", rd);
printf("main: rs = %d\n", rs);
printf("\n");
rd = 10;
rs = 11;
demo();
func();
printf("\n");
printf("main: rd = %d\n", rd);
printf("main: rs = %d\n", rs);
printf("\n");
return 0;
}
operation result:
demo: d = 0
func: s = 0
main: rd = -2
main: rs = 0
demo: d = 0
func: s = 11
main: rd = -2
main: rs = 11
请按任意键继续. . .
The variable represented by rd has been destroyed when the function returns. At this time, rd is a meaningless wild pointer. So -2 is actually a random value.