The periods (.), colons (:), and double colons (: :) operators in c ++

Source: Internet
Author: User

The periods (.), colons (:), and double colons (: :) operators in c ++
1. colon (:) usage

(1) represents the definition of the intra-organization bit domain (that is, the variable occupies several bit spaces)

Typedef struct _ XXX {

Unsigned char a: 4;

Unsigned char c;

} XXX;

(2) The colon following the constructor acts as a segmentation function. It is a class-based method for assigning values to member variables. The initialization list is more suitable for the constant const type of member variables.

Struct _ XXX {

_ XXX (): y (0xc0 ){}

};

(3) The colons after public: AND private: indicate that all the Members defined later are public or private until the next "public:" or "private:" appears. "Private:" is the default processing.

(4) the class name is followed by the colon to define the inheritance of the class.

Class derived class name: Inheritance Method Base class Name
{
Member of the derived class
};
Inheritance Method: public, private, and protected. The default process is public.
 

2. Double colon (: :) usage

(1) "domain operator"
For example, A Class A is declared, and Class A declares A member function void f (), but the definition of f is not given in the class declaration, when defining f outside the class, it must be written as void A: f (), indicating that the f () function is A member function of Class.
(2) It is used directly before the global function to indicate that it is a global function.
For example, in VC, you can add ::
(3) Reference member functions and variables, and scope member operators
For example, System: Math: Sqrt () is equivalent to System. Math. Sqrt ()
--------------------------------------

VC:
: It is the "Scope decomposition operator" in C ++ ". For example, if A Class A is declared and Class A declares A member function voidf (), but f is not defined in the class declaration, f is defined outside the class, voidA: f () indicates that the f () function is A member function of Class.
: Generally, a global function is used directly before a global function. When a member function of a class has the same name as a global function outside the class, the examination prompts that when the class is defined, this function name calls its own member function by default; if you want to call a global function with the same name, you must add: to show the difference. For example, in VC, you can add: Before the API function name when calling the API function ::.

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

This article will summarize the usage of colons in C/C ++.


1. Bit domain definition
I have mentioned enough in the previous article about bitwise struct and I will not repeat it any more.

2. Initialization list of Constructor
Let's talk about what a constructor is? C ++ people should know about it, just in case ). The so-called constructor is a function with the same name as a class. It differs from a common function in that it has no return type.
The constructor is followed by a colon and the initialization list is added. Each initialization variable is separated by a comma. The following is an example.

Class myClass {public: myClass (); // constructor. There is no return type and a list of parameters is available. skip this step ~ MyClass (); // destructor int a; const int B;} myClass: myClass (): a (1), B (1) // initialization list {}
The preceding example shows the usage of colons. The following describes the usage of colons:
1) The initialization list is equivalent to assigning values to the corresponding member variables in the constructor, but there is a difference between the two.
Variables are initialized in the initialization list, while values are assigned in the constructor. The difference between the two is particularly evident in operations on data of the const type. We know that variables of the const type must be initialized at definition, but cannot be assigned values to variables of the const type. Therefore, member variables of the const type can only (and must) perform initialization in the initialization list, that is, the following code will fail:
MyClass: myClass () {a = 1; // yes. The effect is equivalent to initializing B = 1 in the initialization list; // The const variable cannot be assigned a value ;}
2) The initialization sequence is the same as that of the member variable name.
First, let's look at the program below:
myClass::myClass():b(1),a(b){}
What are the results of Execution a and B? B = 1, a = 1? No, B = 1, and a is a random number. This is very important. Generally, during initialization in the initialization list, the initialization sequence should be consistent with the declared sequence to prevent unnecessary errors.
3) For inherited classes, you can initialize the base classes in the initialization list. The initialization sequence is to initialize the base class first, and then initialize the Class Based on the declared sequence of its own variables.

3. Declare the base class.
Suppose we redefine a class and inherit from the myClass class. The definition is as follows:
Class derivedClass: public myClass
{
// Skip
}
Here, the colon serves as the base class of the sound name. You can add public, private, protected, and other labels before the base class name to identify or omit the inherited type, if this parameter is omitted, the class defined by class is private by default, and the class defined by struct is public by default. The differences between labels are not mentioned here.
Similar to the initialization list, multiple base classes can be named here. Each base class is separated by commas.

4. Condition Statement (? :)
And? A condition statement is equivalent to if else, as shown below;
Int a, B, c;
A = 3;
B = 2;
C = a> B? A: B; // If a> B is true, a is assigned to c. Otherwise, B is assigned to c.
The structure of the Condition Statement is:
Conditional expression? Expression 1: expression 2
When the conditional expression is true, the value of the expression is the value of expression 1, otherwise it is the value of expression 2.
Notes:
1 )? : It can be nested, but it is not recommended (difficult to understand). What do you mean by the following expressions?
Int max = I> j? I> k? I: k: j> k? J: k;
Your head is too big, huh, huh.
2 )? : It has a low priority. Please note that what is the execution result of the following program?
Int I = 3;
Int j = 2;
Cout <I> j? I: j; // error, <ratio> has a higher priority, and the execution order is (cout < J )? I: j, equivalent to cout < Cout <(I> j )? I: j; // output 1 or 0, equivalent to (cout <(I> j) as the judgment condition to determine the value of the expression as I or j, for cout <(I> j) and I> j, 1 is output; otherwise, 0 is output. Then (cout <(I> j) is used? : Condition. If cout is correctly executed, it is 1 (true). Otherwise, it is 0 (false). In this way, 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, otherwise false.
More questions about priority are not mentioned.

5. Statement label
Usually used with goto, such:
Step 1: a = f1 ();
....
Goto step1;
This method is not very recommended because it destroys the sequential execution of statements. You should be clear about the cost. However, it is reasonable to exist. Since it still exists, it is certainly helpful to have it. For example, multi-layer nested exit (which is more intuitive than break continue ), it can also avoid repeated code and the like.

6. After case in the switch statement.
I can't say anything if I don't.

7. assembly instruction Template
This I do not understand, do not shift door Axe, you can refer to: http://developer.e800.com.cn/articles/2006/43/1144846933898_1.html
Study in another day.

 

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

1,
Scope Symbol: the class name is generally followed by the member name of the class. C ++ is used as an example to prevent different classes from being distinguished by the scope when they have members with the same name.
For example, A and B indicate two classes. both A and B have members member. So
A: member indicates the member in Class.

B: member indicates a member of Class B.

 

2,

Global scope Symbol: when a global variable has the same name as a certain variable in a local function, you can use: to distinguish it, for example:

 

Char zhou; // global Variable void sleep () {char zhou; // local variable char (local variable) = char (local variable) * char (local variable );:: char (global variable) =: char (global variable) * char (local variable );}
3,

 

: It is the "Scope decomposition operator" in C ++ ". For example, if A Class A is declared and Class A declares A member function voidf (), but f is not defined in the class declaration, f is defined outside the class, voidA: f () indicates that the f () function is A member function of Class. For example

 

Class CA {public: int ca_var; int add (int a, int B); int add (int a) ;}; // This function must be written as follows: int CA: add (int a, int B) {return a + B;} // In addition, double colons are often used to represent the elements of the current class instance in the class variable, such as: int CA: add (int a) {return a +: ca_var ;} // indicates the variable ca_var in the current class instance.

 

The period (.) Operator in C ++

 

First, we will introduce the structure in C ++. For a structure,
Struct MyStruct {
Int member_a;
};

If there is a variable MyStruct s, you can use
S. member_a = 1;

If you use the pointer Method for access, such as MyStruct * ps, the same access must use the arrow Number:

Ps-> member_a = 1;

::It is only used in class member functions and class member variables. For example, declare a class:

 

class CA {          public:          int ca_var;          int add(int a, int b);          int add(int a);};

To implement this function, you must write it as follows:
Int CA: add (int a, int B)
{
Return a + B;
}

 

In addition, double colons are often used to represent the elements of the current class instance in the class variable, such:

Int CA: add (int)
{
Return a +: ca_var;
}

The variable ca_var in the current class instance.

In c ++, when defining a class object as a pointer object, you need to use-> point to a member in the class. when defining a general object, you need to use ". "pointing to a member in the class .......
For example:

 

class A{    public:           play();}

 

If the definition is as follows:

A * p: p-> play (); Structure pointer on the left.
For A p, use p. paly (); for the left, use the structure variable.


 

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.