C ++ class library

include

using namespace std;

/
Pointers and references:
pointers: pointing to a piece of memory variables. Which itself is the first address information stored in the memory address that points to determine the type of memory size.
Variables: The name of a memory address of a data value.
Quote: Aliases defined variables.
Analogy:
Supermarket cabinet:
1, the first row from the right third -------------- addresses (pointers)
2, cabinet 18 ----------- No --------- variable name
3,18 difficult to remember, homophonic memory is "to send" ---- references
/

// pointer (int , int &, int &, int & *, int **)
// exchange value to Case
// int
void swap1 (A int, int B) {// is passed in parameter
int tmp;
tmp A =;
A = B;
B = tmp;
}
void main01 ()
{
int A =. 1, B = 2;
swap1 (A, B);
COUT << "A =" << endl << A;
COUT << "b =" << b << endl ; // exchange fails, switching the parameter values, the exchange can not be achieved.
System ( "PAUSE");
}
// Summary: parameter can not be changed at the arguments, pointers can.
/
Because the parameter argument is passed to a new variable defined subfunctions when the initial time value equal to the
pointer when the value of the address, but not passed argument change is the value of the argument.
/

int //
void swap2 (int
A, int B) {// is passed in argument address
int tmp;
tmp =
A;
A = B;
B = tmp;
}
void main02 ()
{
int =. 1 A, B 2 =;
swap2 (& A, & B);
COUT << "A =" << endl << A;
COUT << "B =" B << << endl; // successful exchange
System ( "PAUSE");
}
// issue 1: int
A and int * a difference there? A: No difference.

// int &
void swap3 (int & A, int & B) {// references passed in the argument
int tmp;
tmp = A;
A = B;
B = tmp;
}
void main03 () {
int A =. 1;
int 2 = B;
swap3 (A, B);
COUT << "A =" << endl << A;
COUT << "B =" B << << endl; // exchange is successful, i.e., the alias reference
system ( " PAUSE ");
}
// issue 1: int & a and int & a there is no difference? A: No difference.

int // &
void swap4 (int
& a, int & b) {// passed is the address of a, where a & a representation of an alias, but it is still a thing. Think 18 "to send" relationship and
int tmp =
a; // operation inside and a like number of cabinet as a whole can be understood as the relationship between supermarkets and 18 "to be made" in.
= A b; // passed an alias address?
tmp = B;
}
void main () {
int = A. 1;
int B = 2;

int *aPtr = &a;    //用aPtr存放a地址
int *bPtr = &b;    
swap4(aPtr, bPtr);

cout << "a=" << a << endl;
cout << "b=" << b << endl;   //交换成功
system("pause");

}

int & //
// void swap5 (int & a
, & int B) {} // C ++ define failed
@ Summary: No & a
form

int //
void swap6 (int
A, int B) {// purpose is to operate on the arguments, here is the address of the incoming address
int tmp;
tmp =
A;
A = B;
** B = tmp;
}
void main06 () {
int A =. 1, B = 2;

int *aPtr = &a;  //存放变量a的地址  
int *bPtr = &b;  

int **aPtr2 = &aPtr;  //存放变量a的地址的地址
int **bPtr2 = &bPtr;
swap6(aPtr2, bPtr2);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");

}
/
Question 1: the address can not be stored directly to a variable, i.e., int b = & a. why?
A: When you perform certain operations, such as incrementing +1 +1 is not, and second, plus an integer variable needs, so they need to address to operate. I.e.,
b = & a ;.
Summary: variable address can not be stored directly, it needs to be stored by indirect pointer. Store the address of a variable format: int A = & B;
/

/
Summary:
. 1, void swap1 (A int, int B) ---------- parameter is passed, not the actual operation of the memory argument.
2, void swap2 (int
A, int B) -------- address argument is passed, the actual operation may be performed on the memory argument.
3, void swap3 (int & a , int & b) -------- reference format, argument passed is the address of the actual memory operation may be performed on the argument. Equivalent arguments have been identified, the internal function arguments can operate directly, without the need to add a non-specific
or &.
. 4, void swap4 (int & A, int & B) ------- passed argument is a pointer to the address, the format operation: External: storage address: B = & A; Internal: A
. 5, swap5 void (int a & , int & B) ------ malformed
. 6, void swap6 (int a, int B) ------ incoming address is an address, the format operation: external: storing an address: B = & a; second address storage: C & B =; internal: a;
to operate memory argument, it is necessary to lock argument. The operation is then changed format.
/

Guess you like

Origin www.cnblogs.com/WinkJie/p/11286341.html