University of C ++ class knowledge (the fourth of the scoping operator cast)

Ha ha ha, finally remembered my CDSN the password (not only for those days did not update excuses, ╭ (╯ ^ ╰) ╮)

 

First, the operator Scope      ::

It is behind the two colons Ha, right, right, is called the scope operator.

Maybe colon le on the powerful, he can put local variables become global variables.

Ahem ~ official explanation is: if there are two variables of the same name, is a global, local and the other is, then the local variable has a higher priority in scope, it will shield the global variable.

Man of few words said, the examples:

全局变量和局部变量同名:


#include <iostream>
using namespace std;
int avar=10;  //全局变量
int main ()
{
    int avar;  //局部变量
    avar=25;
    cout<<"avar is "<<avar<<endl;
    return 0;
}

In the VC output results are as follows:

avar is 25
Press any key to continue

Thus, the output value of the local variables is the main function inside. Therefore, the local variable is greater than the priority of the global variables.

Well, I want to use the scope operator, how to use it?

We look at the following example:

#include<iostream>
using namespace std;
int avar;
int main()
{
    int avar;
    avar=11;
    ::avar=10;
    cout<<"局部变量是"<<avar<<endl;
    cout<<"全局变量是"<<::avar<<endl;
	return 0;
}

VC operating results are:

局部变量是11
全局变量是10
Press any key to continue

 

 

Two , cast

Is the conversion between data types.

In C, it is generally like this:

int i=10;
double x=(doble)i;

As an extension of C ++, C expression is of course support you, but in C ++, the addition of a form:

C++中相比C,就把位置换了一下,看起来像函数调用那么回事:

int 1=10;
double x=double(i);

However, in C ++, which is recommended Oh!

Well, the fundamental part of C ++ code was finally finished, the next one is beginning to enter the code is the C ++ style.

Published 19 original articles · won praise 9 · views 7449

Guess you like

Origin blog.csdn.net/Cryu_xuan/article/details/84072182