Use of colons (:) and Double colons (::) in C + +

Source: Internet
Author: User

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 ()
——————————————————————————————————————

VC in the following

:: Is the scope decomposition operator in C + +. For example, a Class A is declared, a member function VOIDF () is declared in Class A, but no definition of f is given in the declaration of the class, so the definition of F outside the class is written as Voida::f (), which means that the F () function is a member function of Class A.
:: In general, there is a usage that is directly before the global function, which represents the global function. When the member function of a class has the same name as a global function outside the class, when the test is defined in the class, the name of the function is called by default, and if you want to invoke a global function with the same name, you must hit:: To show the difference. For example, in the VC, you can call the API function, in front of the API functions to add:. (Edit:)

*********************************************************************************************

*********************************************************************************************

*********************************************************************************************

This article summarizes the use of colons in C/A + +.

1. bit domain definition
This is more than enough in the previous article about the bit structure, and it is not repeated.

2. Initialization list of class constructors (Constructor)
First, what is called the constructor (is it verbose?) Everyone in C + + should know, just in case. The so-called constructor is a function with the same name as the class, which differs from the normal function in that it has no return type.
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.

3. 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.

4. 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&GT;J)) as a decision condition, to determine the value of an expression of I or J, and cout<< (I&GT;J),i> J Output 1 Otherwise 0, then (cout<< (I&GT;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.

5. 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

6. In the switch statement, after the case.
If not, I have no words to say.

7. Assembly Instruction Template
This I do not understand, do not swim, you can refer to: http://developer.e800.com.cn/articles/2006/43/1144846933898_1.html
Learn some other day.

*********************************************************************************************

*********************************************************************************************

*********************************************************************************************

1.
Scope symbol:: The front of the general is the class name, followed by the class is generally the member name, C + + as an example to avoid different classes have the same name of the member and the scope of the way to differentiate
For example: A, B represents two classes, and a member is member in a A, B. So
A::member represents a member of Class A member
B::member represents a member in Class B member



2.
Global scope Symbol: When a global variable has the same name as one of the variables in a local function, you can use:: To distinguish between:

Char Zhou; Global variables

void Sleep ()



Char Zhou; Local variables

char (local variable) = char (local variable) *char (local variable);

:: char (global variable) =::char (global variable) *char (local variable);




3.
:: Is the scope decomposition operator in C + +. For example, a Class A is declared, a member function VOIDF () is declared in Class A, but no definition of f is given in the declaration of the class, so the definition of F outside the class is written as Voida::f (), which means that the F () function is a member function of Class A. For example

[CPP]View Plaincopy
  1. The. class CA {
  2. Public :
  3. Ca_var int;
  4. int Add (int A, int b);
  5. int Add (int a);
  6. 06};
  7. 07
  8. //So when you implement this function, you must write this:
  9. ca::add Int (intA, int b)
  10. 10 {
  11. return a + B;
  12. 12}
  13. 13
  14. + //Also, double colons are often used to represent elements within a class variable as the current class instance, such as:
  15. int Ca::add (int a)
  16. 16 {
  17. + return a +:: Ca_var;
  18. 18}
  19. 19
  20. A//represents the variable Ca_var in the current class instance.

Use of colons (:) and Double colons (::) in C + +

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.