Non-object-oriented features of C + + extensions (3)

Source: Internet
Author: User

Today's talk is about the C + + scope operator "::", which enforces the expansion of type conversions, the operator new and delete in C + + for malloc and free functions in C + +, and a significant extension of C + + to C.: reference (reference); This is also C + + The last section of the series extends the non-object-oriented nature of the C language.

1. If there are two variables of the same name, one is global and one is local, then local variables have higher precedence in their scope, and global variables are masked. What if I want to use a global variable in the scope of the local variable, then we need to use: scope operator. Like what:

1 #include <iostream>
2
3 UsingNamespace std;
4
5 int x;
6
7 int Main ()
8
9 {
10
one int x;
12
x=50;
14
: x=100;
16
cout<< "local variable x=" <<x<<endl;
18
cout<< "global variable x=" <<::x<<endl;
20
Return0;
22
23}

Results:

  

2. There is a forced type conversion in C language such as: int x=1;double y= (double) x, while C + + supports this format and provides a conversion similar to the function format: int x=1;double y=double (x);

The malloc and free functions in 3.C are used to dynamically allocate memory and release dynamically allocated memory, whereas in C + +, both functions are preserved, and operator new and delete are used to better allocate and release memory. Basic form of memory allocation: pointer variable name =new type, such as: int *x;x=new int, or char *chr;chr=new char; free memory (delete pointer variable name): Delete X;delete chr; Although the functions of new and delete are similar to malloc and free, the former has several advantages: (1) New can automatically calculate the amount of memory to allocate based on the data type, and malloc must use the sizeof function to calculate the required bytes; (2) New can automatically return the correct type of pointer, and the return value of malloc is always void*, and must be cast in the program;

New can dynamically allocate memory space for an array such as: int *array=new int[10] or int *xyz=new int[8][9][10]; release with delete []array and delete []xyz; In addition, new can be initialized when allocating memory to a simple variable, such as int *x=new int (100);

The data can be initialized, and sometimes there is not enough memory to satisfy the allocation requirements, some compiled systems will return null pointers, such as:

1 #include <iostream>
2
3 UsingNamespace std;
4 int Main ()
5 {
6 int*x;
7 X=newint;
8 if (!x)
9 {
Ten cout<< "failed to allocate memory! "<<endl;
One by one return1;
12}
*x=10;
cout<<*x;
Delete x;
Return0;
17}

4. In the next step, refer to the C + + reference (reference), first explain what is a reference? For example, a person may have three or four names, but what these three or four names do is actually what the other person does. A reference is an alias for a variable. Its format: type & reference name = the variable name defined; for example:

1 #include <iostream>
2
3 UsingNamespace std;
4 int Main ()
5 {
6 int x=100;
7 int&y=x;
8 x=50;
9 cout<< "x=" <<x<<endl;
Ten cout<< "y=" <<y<<endl;
11
y=0;
cout<< "x=" <<x<<endl;
cout<< "y=" <<y<<endl;
15
Return0;
17}

Results:

In fact, the reference and the variable it represents share the same memory unit, the system part refers to allocating additional storage space, and the compilation system makes the reference and the variable it represents have the same address.

1 #include <iostream>
2
3 UsingNamespace std;
4 int Main ()
5 {
6 int x=100;
7 int&y=x;
8 x=50;
9 cout<< "variable x address:" <<&x<<endl;
cout<< "referring to the address of Y:" <<&y<<endl;
One by one return0;
12}

Results:

Found that the reference is actually the case, but there are a few points to note: (1) When declaring a reference, it must be initialized immediately, cannot be declared after the assignment: such as int x=10;int &y;y=x; (2) The type of the reference must be the same as the type of the variable to which it is assigned, not: int X;double &y=x; (3) The value provided for the reference, which can be a variable or a reference: int x=5;int &y=x;int &z=y; (4) Reference cannot be re-declared as another variable after initialization: int x,y;int &z=x;z=&y;

In fact, the main use of reference is as a function of the parameters, review, the previous transfer function parameters in C there are two cases, namely, "Value call" and "call", the former is one-way, the latter is bidirectional, and the reference as a function parameter is passed, it is "address call", It is consistent with the effect that the pointer in C passes as a parameter, except that it does not need to cross the indirect reference operator "*" like a pointer; for example, compare these two methods:

1 #include <iostream>
2
3 UsingNamespace std;
4 void Swap (INT*X,INT*Y)
5 {
6 int temp;
7 temp=*x;
8 *x=*y;
9 *y=temp;
10}
11
void Swap (Int&x,int&y)
13 {
int temp;
Temp=x;
X=y;
Y=temp;
18}
int main ()
20 {
int i=10,j=5;
cout<< "i=" <<i<< "j=" <<j<<endl;
% swap (&AMP;I,&AMP;J);
cout<< "i=" <<i<< "j=" <<j<<endl;
Swap (I,J);
cout<< "i=" <<i<< "j=" <<j<<endl;
Return0;
28}

Results:

For reference, there is a little bit of detail to say: (1) Cannot establish reference array, for example: int a[4]= "ABCD"; int &araay[4]=a; (2) Cannot establish reference reference, cannot establish pointer to reference, for example: int x=50;int &&y=x;int &z=x;int *p=z; (3) A reference address can be assigned to a pointer, (4) a reference can be qualified with a const, and the referenced value is not allowed to be changed, for example: int x=10;const int &t=x;t=5 , but x=5 can, at this time X and T are equal to 5, (5) The reference operator and the address operator is &, but the reference is only used when declaring, and other occasions use & is the address operator! For example: int x=5;int &y=x;y=10;int *z=&y//& is the address operator;cout<<&z//& is the address operator;

Finally, let's use an example to summarize what we're talking about today (development tools: VS2010):

1 #include "stdafx.h"
2 #include <iostream>
3
4 UsingNamespace std;
5
6 int x=10;//global variable
7 void Swap (INT*X,INT*Y)//parameter of pointer type
8 {
9 int temp;
Ten temp=*x;
One by one *x=*y;
*y=temp;
13}
14
void Swap (INT&AMP;X,INT&AMP;Y)//parameter with reference type
16 {
-int temp;
Temp=x;
X=y;
Y=temp;
21}
int main ()
23 {
Double*y=newdouble (5.55);//new dynamically allocates memory space, the byte size is the same as the byte of the double type, and initializes the value
x=double Int (*y);//coercion type conversion
26
cout<< "local variable x=" <<x<< "global variable x=" <<::x<<endl;
-Swap (&AMP;X,&AMP;::X);
cout<< "local variable x=" <<x<< "global variable x=" <<::x<<endl;
Swap (X,::X);
cout<< "local variable x=" <<x<< "global variable x=" <<::x<<endl;
32
Delete y;//free memory space
Return0;
35}

Results:

 

Non-object-oriented features of C + + extensions (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.