Transfer from http://www.360doc.com/content/13/0605/11/3373961_290615318.shtml
1. Colon (:) usage
(1) Represents the definition of a bit field within a mechanism (that is, the variable occupies several bit spaces)
typedef struct _xxx{
unsigned char a:4;
unsigned char c;
} ; Xxx
(2) The colon at the back of the constructor functions as a method of assigning a value to a member variable, initializing the list, and more appropriate for the const type of the member variable.
struct _xxx{
_xxx (): Y (0xc0) {}
};
(3) Public: and private: The following colon, which indicates that all the members defined later are publicly or privately held until the next "common:" or "Private:" appears. "Private:" is handled by default.
(4) The class name followed by a colon is used to define the inheritance of the class.
Class derived class Name: Inheritance mode base class name
{
Members of derived classes
};
Inheritance mode: Public, private, and protected, default processing is public.
2. Double colon (::) usage
(1) indicates "domain Operator"
Example: Declares a Class A, Class A declares a member function void F (), but does not give the definition of F in the declaration of the class, so when F is defined outside the class,
will be written as void A::f (), which indicates that the F () function is a member function of class A.
(2) Directly before the global function, which means the global function
Example: In the VC, you can call the API function, in front of the API functions:
(3) Representing reference member functions and variables, scope member operators
Example: System::math::sqrt () equivalent to SYSTEM.MATH.SQRT ()
I. Single colon (:)
While some information is stored, it does not need to occupy a full byte, only a few or one bits. For example, when storing a switching volume, there are only 0 and 12 states, with one binary. In order to save storage space and make processing simple, C language also provides a data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in one byte into several different regions and describe the number of bits per region. Each domain has a domain name that allows operations to be performed by domain name in the program. This allows you to represent several different objects in a bits field of one byte. The definition of a bit field and the description of a bit-field variable are similar to the structure definition, in the form of:
struct bit domain structure name
{bit field List};
Where the list of bit fields is in the form: type specifier bit domain name: bit field length
For example:
struct BS
{
int a:8;
int b:2;
int c:6;
};
The description of a bit-field variable is the same as the structure variable description. Can be defined by the first description, at the same time define the description or direct description of the three ways. For example:
struct BS
{
int a:8;
int b:2;
int c:6;
}data;
Indicates that data is a BS variable, which accounts for two bytes. Where bit domain A occupies 8 bits, bit domain B is 2 bits, bit field C is 6 bits. There are several explanations for the definition of bit fields:
1. A bit field must be stored in the same byte and cannot span two bytes. If one byte has enough space left to hold another domain, the bit field should be stored from the next cell. You can also intentionally make a field start from the next unit. For example:
struct BS
{
unsigned a:4
unsigned:0/* airspace */
Unsigned b:4/* Start storage from the next unit */
unsigned c:4
}
In this bit domain definition, a occupies the first byte of 4 bits, the latter 4 bits fill 0 means not to use, B starts with the second byte, occupies 4 bits, and C occupies 4 bits.
2. A bit field can have no bit domain name, it is only used to fill or adjust the location. Nameless bit fields are not available. For example:
struct k
{
int a:1
Int:2/* The 2-bit cannot be used */
int B:3
int C:2
};
From the above analysis, we can see that the bit field is essentially a type of structure, but its members are assigned by binary.
3. Conditional statement (?:)
constitute a conditional statement that acts as if else, as follows;
int a,b,c;
A=3;
b=2;
c=a>b?a:b;//If A>B is established, the anti-a assigns to C, otherwise assigns B to C
The structure of the conditional statement is:
Conditional expressions? Expression 1: Expression 2
When the conditional expression is true, the value of the expression is the value of expression 1, otherwise the value of expression 2.
A few notes:
1): Can be nested, but not recommended (difficult to understand), the following expression can you understand what the meaning of it?
int max = I>j? I>k? I:k: J>k? J:k;
The head is big, hehe.
2)?: With a very low priority, this should pay attention to Oh, the following program execution result is what?
int i = 3;
int j = 2;
cout << i>j?i:j;//error,<< higher priority than >, execution Order ((cout<<i) >j)? I:j, equivalent to the size of Cout<<i and J, Then, depending on the result of the comparison, the expression value is either I or J, which is obviously wrong, the value of Cout<<i is cout and cannot be compared with the integer J.
cout << (i>j) i:j;//output 1 or 0, equivalent to (cout<< (I>J)) as a decision condition, to determine the value of an expression of I or J, and cout<< (I>J),i> J Output 1 Otherwise 0, then (cout<< (I>J)) as? : The condition, if cout is correctly executed, is 1 (true), otherwise 0 (false), which determines that the expression value is I or J
cout << (i>j?i:j);//i>j output I, otherwise output J, expression value TRUE if cout is executed correctly, false
More questions about priorities are not to be said.
4. Statement label
Usually used in conjunction with Goto, such as:
Step1:a = F1 ();
....
Goto Step1;
This is also not recommended, because it destroys the sequential execution of statements, so the cost should be clear. But the existence is reasonable, since it still exists, there must be its usefulness has its advantages, for example, multi-layered nesting exit (will be more intuitive than break continue), can also avoid repeating the code and the like
5. In the switch statement, after the case.
If not, I have no words to say.
6. Assembly Instruction Template
Specific reference: http://developer.e800.com.cn/articles/2006/43/1144846933898_1.html
7. Initialization list of class constructors (Constructor)
After the constructor is followed by a colon plus an initialization list, each initialization variable is separated by a comma (,). Let's give an example.
Class MyClass
{
Public:
MyClass ();//constructor, no return type, can have parameter list, omitted here
~myclass ();//destructor
int A;
const int B;
}
Myclass::myclass (): A (1), B (1)//initialization list
{
}
The example above shows this usage of the colon, which is explained in several ways:
1) The function of the initialization list is equivalent to assigning the corresponding member variable within the constructor, but there is a difference between the two.
In the initialization list, the variable is initialized, and the assignment is performed within the constructor. The difference between the two is particularly noticeable for operations like const type data. We know that a variable of the const type must be initialized at the time of definition, not a const type variable, so a member variable of the const type can only (and must) be initialized in the initialization list, that is, the following code will be faulted:
Myclass::myclass ()
{
A = 1;//Yes, the effect is equivalent to initializing in the initialization list
b = 1;//error, const variable cannot be assigned operation;
}
2) The order of initialization is the same as the order of the member variable's fame.
First look at the following program:
Myclass::myclass (): B (1), A (b)
{
}
What is the result of such an implementation, A, B? B=1,a=1? No, B=1 and A is a random number. This is very important. In general, when initializing in the initialization list, the order of initialization should be consistent with the order of the declarations, preventing unnecessary errors.
3) for inherited classes, the initialization of the base class is also possible in the initialization list, initialized in the order that the base class is initialized, and then initialized according to the order in which the class's own variables are declared.
8. Declare the base class.
Suppose we redefine a class that inherits from the MyClass class. This is defined in the following way:
Class Derivedclass:public MyClass
{
Omitted
}
The colon here is the role of the famous base class, in front of the base class name can be added public\private\protected and other tags, used to identify the type of inheritance, can also be omitted, omitted, classes defined by the class default to private, Classes defined with structs default to public, and there is no difference between specific labels.
As with the initialization list, you can also declare multiple base classes, separated by commas (,) between the base classes.
Two Double colon (::) usage
1. Indicates "domain Operator"
Example: Declares a Class A, Class A declares a member function void F (), but does not give the definition of F in the declaration of the class, so when F is defined outside the class,
will be written as void A::f (), which indicates that the F () function is a member function of class A.
2. Directly before the global function, which means the global function
Example: In the VC, you can call the API function, in front of the API functions:
3. Representing reference member functions and variables, scope member operators
Example: System::math::sqrt () equivalent to SYSTEM.MATH.SQRT ()
4. A namespace-scoped character, which is 2 directly before the global function, represents a global function
The highest level in the operator hierarchy!
Using namespace namespace name (e.g., ABC);
Indicates that the identifier used in the following program code (if this identifier is defined in ABC) is in ABC, including the type name (class), variable name, function name, object name ...
Using ABC:: identifier (i);
Only indicates that the identifier I used in the following code is in ABC.
If you want to use multiple identifiers in ABC, you can only use
Using Abc::a;
Using Abc::b;
Using Abc::c;
...
Wait for one of them to list it!
Of course, using the using statement is more convenient
But not safe.
(1) using namespace; In the event that the identifier of the same name is used in a different two namespace, the system cannot determine which namespace the identifier belongs to;
(2) using ABC::; In case your program also uses a function (the function name is the same as the one in ABC), then the system cannot tell whether you are using the function in ABC or the one in this program;
The safest approach (and, of course, the most cumbersome)
Is that whenever you use a variable (function ...) , you have to be clear about his origin (that is, which namespace) unless it has no namespace
For example:
#include <iostream>
int main ()
{
Std::cout << "Hello, world!" << Std::endl;
}
Two objects in the iostream file are used here (Cout,endl)
Because most of the functions in the C + + standard library, objects ... are placed in the namespace Std.
So
The code above is equivalent to
#include <iostream>
Using Std::cout;
Using Std::endl;
int main ()
{
cout << "Hello, world!" << Endl;
}
#include <iostream>
using namespace Std;
int main ()
{
cout << "Hello, world!" << Endl;
}
To give an example again:
#include <iostream>
#include <string>
int main ()
{
int A;
Std::string b;
Std::cin >> A;
Std::cin >> B;
Std::cout << "Hello, world!" << Std::endl;
return 0;
}
1) using std::;
#include <iostream>
#include <string>
Using Std::cin;
Using Std::endl;
Using Std::string;
int main ()
{
int A;
String B;
Cin >> A;
CIN >> B;
Std::cout << "Hello, world!" << Endl; Note that cout does not use using
return 0;
}
2) using Namespace;
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
int A;
String B;
Cin >> A;
CIN >> B;
cout << "Hello, world!" << Endl;
return 0;
}
where cout Endl cin string is in STD!
Other:
C + + scope operators:: There are 2 main ways to apply.
First, declaring a Class A, Class A declares a member function void F (), but does not give the definition of F in the declaration of the class, then when defining f outside of the class, it is written as void A::f (), which means that the F () function is a member function of class A.
Secondly, the scope can be
Simple: global scope, local scope, statement scope
Scope Priority: the smaller the scope, the higher the priority
Scope operator: "::"
If you want to use global variables of the same name within the scope of a local variable, you can precede the variable with "::", "::" called the scope operator.
eg
Scope # include <iostream>using namespace Std;int avar=10; Global variable Avarint main () {int avar=20;//local variable avarcout<< "Avar is:" <<avar<<endl; Access local variable avar=25; 1//Modify local variable cout<< "Avar is:" <<avar<<endl;cout<< "Avar is:" <<::avar<<endl; Access Global variables:: avar=30; 2//Modify global variable cout<< "Avar is:" <<::avar<<endl;return 0;}
The output results are: 20,25,10,30
C + + Colon action