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

Reprinted from: http://fengqing888.blog.163.com/blog/static/3301141620100623933512/

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.