Double colon action in C + +

Source: Internet
Author: User

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:

For example:

#include <stdio.h>int count = 0; Global variable 0void main () {  int count = 10;//local variable  of printf ("Count =%d\n", count);//local variable masks global variable so the output is ten {  int Count =;    printf ("Count =%d\n",:: Count); Scope symbol indicates reference global variable 0  //::sort}}

As a global scope symbol, double colons are placed at the beginning. This is similar to the file path under Linux, where "/" is placed at the beginning to represent an absolute path, and is placed in the middle to represent a subdirectory.

:: A symbol can specify either a class or a variable and a namespace:

If a function is placed globally, the global function is the global namespace if the variable is defined globally, if a namespace is placed globally.
3.

另外,双冒号也常常用于在类变量内部作为当前类实例的元素进行表示:

Class CA {public:  int ca_var;  int add (int a, int b);  int add (int a);}; scope int Ca::add (int a, int b) {  return a + B;}//In addition, a double colon is represented inside a class variable as an element of the current class instance int ca::add (int a) {  return a +:: Ca_var ;} Represents a variable in the current class instance Ca_var



Double colon action 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.