Dot (.), colon (:), and double-colon (::) operators in C + +

Source: Internet
Author: User

1. Usage of colon (:)
(1) Represents the definition of the bit field in the organization (ie, the variable occupies several bit spaces)

typedef struct _XXX {

             unsigned char a: 4; // char type a takes 4 digits

             unsigned char c;

} XXX;

(2) The colon behind the constructor functions as a split, and is a method for the class to assign 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 colon behind public: and private: means that all the members defined later are public or private until the next "public:" or "private:" appears. "private:" is the default.

(4) The colon behind the class name is used to define the inheritance of the class.
 class derived class name: inheritance method base class name
{
        Members of derived classes
};
Inheritance: public, private and protected, the default processing is public.


2. Usage of double colon (: :)
(1) means "domain operator"
        Example: A class A is declared, a member function void f () is declared in class A, but the definition of f is not given in the class declaration, then when defining f outside the class, it must be written as void A :: f (), Indicating that this f () function is a member function of class A.
(2) Used directly before the global function, indicating that it is a global function
        Example: In VC, you can add :: before the API function name in the call API function
(3) Referencing member functions and variables, scope member operator
        Example: System :: Math :: Sqrt () is equivalent to System.Math.Sqrt ()
——————————————————————————————————————————

The following in VC
      :: is the "scope decomposition operator" in C ++. For example, if a class A is declared, a member function voidf () is declared in class A, but no definition of f is given in the class declaration, then when defining f outside the class, it should be written as voidA :: f (), Indicates that the f () function is a member function of class A.
:: There is generally another usage, that is, it is used directly before the global function, indicating that it is a global function. When a member function of a class has the same name as a global function outside the class, when the exam is defined in the class, the function name is called by default to call its own member function; if you want to call a global function of the same name, you must Mark :: 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 field definition
This has been said enough in the previous article on bit structures and will not be repeated.

2. The initialization list of class constructor (Constructor)
Let me talk about what is the constructor function (is it verbose? C ++ people should know it, or just in case). The so-called constructor is a function with the same name as the class. The difference between it and the ordinary function is that it has no return type.
The colon is followed by a colon plus an initialization list, and each initialization variable is separated by a comma (,). Here is 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 above example shows the usage of the colon. Here are some explanations for this usage:
1) The function of the initialization list is equivalent to the assignment of corresponding member variables in the constructor, but there is a difference between the two.
In the initialization list, the variables are initialized, and the assignment operation is performed in the constructor. The difference between the two is particularly evident in operations like const type data. We know that variables of const type must be initialized at the time of definition, and cannot be assigned to variables of const type, so member variables of const type can only (and must) be initialized in the initialization list, that is, the following code will be wrong :
myClass :: myClass ()
{
     a = 1; // Yes, the effect is equivalent to initialization in the initialization list
     b = 1; // Error, const variable cannot be assigned;
}
2) The order of initialization is the same as the order of declaration of member variables.
     First look at the following program:
myClass :: myClass (): b (1), a (b)
{
}
What are the execution results a and b? b = 1, a = 1? No, b = 1 and a is a random number. This is very important. Generally, when initializing in the initialization list, the order of initialization should be consistent with the order of declaration to prevent unnecessary errors.
3) For inherited classes, the base class can also be initialized in the initialization list. The order of initialization is to initialize the base class first, and then to initialize according to the declaration order of the class's own variables.

3. Declare the base class.
Suppose we redefine a class, inherited from myClass class. The definition is as follows:
class derivedClass: public myClass
{
       // omitted
}
The colon here plays the role of a base class. You can add public / private / protected and other labels in front of the base class name to identify the type of inheritance. It can also be omitted. If omitted, the class defined by class defaults to Private, the class defined by struct is public by default, as for the difference between specific tags, I will not say it here.
As with the initialization list, you can also declare multiple base classes, separated by commas (,).

4. Conditional statements (? :)
And? Constitute a conditional statement, which is equivalent to if else, as follows;
int a, b, c;
a = 3;
b = 2;
c = a> b? a: b; // If a> b holds, then a is assigned to c, otherwise b is assigned to c
The structure of the conditional 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.
A few notes:
1) ?: It can be nested, but it is not recommended (difficult to understand). Can you understand the following expression?
int max = i> j? i> k? i: k: j> k? j: k;
Big head, huh, huh.
2) ?: Has a very low priority, this should pay attention, what is the result of the following program execution?
int i = 3;
int j = 2;
cout << i> j? i: j; // Error, <<<> has higher priority, the execution order is ((cout << i)> j)? i: j, which is equivalent to comparing cout << The size of i and j, and then determine the expression value as i or j according to the comparison result, which is obviously wrong. The value of cout << i is cout, which cannot be compared with the integer j.
cout << (i> j)? i: j; // Output 1 or 0, which is equivalent to (cout << (i> j)) as the judgment condition to determine the expression value i or j, and cout < <(i> j), i> j outputs 1 otherwise 0, and then (cout << (i> j)) as? : The condition, if cout is executed correctly, it is 1 (true), otherwise it is 0 (false), to determine the expression value i or j
cout << (i> j? i: j); // i> j then output i, otherwise output j, the expression value is true if cout executes correctly, otherwise false
Not to mention more about priority.

5. Statement label
Usually used in conjunction with goto, such as:
step1: a = f1 ();
....
goto step1;
This approach is not very recommended, because it destroys the order of execution of statements, such a price should be clear to everyone. However, it is reasonable to exist. Since it still exists, it must still have its uses and benefits, for example, multiple nested exits (it will be more intuitive than break continue), and you can also avoid repeating code. some type of

6. After the case in the switch statement.
If I don't say this, if I don't, I have nothing to say.

7. Assembly instruction template
I do n’t understand this anymore. If you do n’t work in the same way, you can refer to it: http://developer.e800.com.cn/articles/2006/43/1144846933898_1.html
Learn another day.
************************************************** *******************************************

1, 
Scope symbol :: is generally in front of the class name, followed by the name of the member of the class, C + + as an example to avoid different classes have the same name members and use the scope to distinguish
For example, A and B represent two classes, and there are members in A and B. Then
A :: member means member in class A
B :: member means member member in class B



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 variable

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, if a class A is declared, a member function voidf () is declared in class A, but no definition of f is given in the class declaration, then when defining f outside the class, it should be written as voidA :: f (), Indicates that the f () function is a member function of class A. E.g


class CA {
public:
int ca_var;
int add (int a, int b);
int add (int a);
};
The
// So when implementing this function, you must write like this:
int CA :: add (int a, int b)
{
return a + b;
}
The
// In addition, the double colon is also often used as an element of the current class instance within a class variable, such as:
int CA :: add (int a)
{
return a + :: ca_var;
}
The
// Indicates the variable ca_var in the current class instance.
  
The dot (.) Operator in C ++
First introduce the structure in C ++. For a structure,
struct MyStruct {
         int member_a;
};

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

If you use the pointer method to access, such as MyStruct * ps, then the same access must use the arrow:

ps-> member_a = 1;

:: 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);
};

Then when implementing this function, it must be written like this:
int CA :: add (int a, int b)
{
     return a + b;
}
In addition, the double colon is also often used to represent the element of the current class instance within the class variable, such as:

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

Represents the variable ca_var in the current class instance.

When defining a class object as a pointer object in c ++, you need to use-> to point to a member in the class; when defining a general object, you need to use "." to point to a member in the class ...
E.g:
class A
{
    public:
          play ();
}
If the definition is as follows:

A * p: p-> play () is used; on the left is the structure pointer.
A p is: p.paly () is used; on the left are structure variables.


The dot (.), colon (:) and double colon (: :) operators in c ++


Related Article

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.