C++ double colon :: Function analysis

  • 2020-06-07 04:53:51
  • OfStack

The most commonly used characters in C++ are:, which are used as follows:

The & # 8226; Scope qualifier. When functions are declared in the body of a class and defined outside the body of a class, they must be defined with the class name and scope qualifier.


class MyClass{ 
 public: 
  int num; 
  int fun();// Class declares functions in the body  
}; 
// Class defines functions outside the body  
int MyClass::fun(){ 
 return 1; 
} 

The & # 8226; Static data and static member function members can be referenced by either the object name or the class name plus ::. For example, MyClass::a. The purpose of static member functions is to manipulate static data members. Static member functions cannot refer to non-static data members.


class MyClass{ 
 private static int a; 
}; 
// Class references static member variables outside of the class  
MyClass::a; 

The & # 8226; References the new typedef type defined in the class


class MyClass{ 
 public typedef int INT; // here typedef Defines a type alias  
} 
MyClass ::INT b;// Class in vitro declaration 1 A variable  

The & # 8226; Global scope symbol: When a global variable has the same name as one of the variables in a local function, it can be distinguished by ::.


int a;// The global variable  
void fun(){ 
 int a; // A local variable  
 a( A local variable ) = a( A local variable )*a( A local variable ) ; 
 ::a( The global variable ) =::a( The global variable ) *a( A local variable ); 
 } 

conclusion


Related articles: