The test paper is to learn from other blog content, recorded in the back to facilitate their own reading review
I. What is the use of const:
Three:
1. Modifying variables, defining read-only variables
2. modifier function parameter or function return value
3. A member function that modifies a class that cannot modify the value of a member variable.
Two. Differences in pointers and references
Reference Links:
Http://www.cnblogs.com/dolphin0520/archive/2011/04/03/2004869.html
Http://www.cnblogs.com/hoodlum1980/archive/2012/06/19/2554270.html
Http://c.biancheng.net/cpp/biancheng/view/169.html
1. The reference is the alias of the variable, the internal implementation is a read-only pointer, and the reference in C + + is implemented by pointers by the compiler, but the implementation transparently handles the programmer at the language level.
The pointer is itself an object that allows the pointer to be assigned and copied
2. References can only be assigned at initialization time, other times cannot be changed, pointers do not have this limit
3. Reference cannot be null, pointer can be null
4. Pointers can have multiple levels and references can be only one level
5.sizeof refers to the size of the pointed variable, and the sizeof pointer gets the size of the pointer itself.
6. When the pointer is passed as a function parameter, the copy of the argument is passed to the formal parameter, such as the following program (a), the reference is passed as a function parameter instead of the copy, and the reference is a true share of the same inner deposit element.
Program (a):
#include <iostream>
#include <stdlib.h>
#include "stdafx.h"
using namespace Std;
void swap (int *a, int *b)
{
int tmp = *A;
*a = *b;
*B = tmp;
int c = 10;
A = &c;
cout << "swap function" << Endl;
cout << "A" is "<< *a <<" "<<" A has a value of "<< a << Endl;
}
int main ()
{
void swap (int *a, int *b);
int a = 4;
int b = 5;
cout << "Pre-change" << Endl;
cout << "A" is "<< &a <<" "<<" A has a value of "<< a << Endl;
cout << "B" is "<< &b <<" "<<" B with the value "<< b << Endl;
Swap (&a, &b);
cout << after "changed" << Endl;
cout << "A" is "<< &a <<" "<<" A has a value of "<< a << Endl;
cout << "B" is "<< &b <<" "<<" B with the value "<< b << Endl;
return 0;
}
The results are as follows:
When pointer A is actually a function argument, it is equivalent to generating a new pointer xxx, except that the pointer xxx points to the same address as the argument pointer A. So when the address of the variable C is assigned to pointer a in the function, it is actually changing the address of the pointer xxx, so the function A = &c; does not affect the real pointer a at all.
C + + Written test-1, basic theory