新手问一个C/C++中的操作符"::"的含义和用法?

解决方案 »

  1.   

    ::是域操作符,指示后面的东西是属于哪个类A::a()表示a是A的成员函数
    如果前面没有东西,则表示全局的
      

  2.   

    恩~
    建议楼主看看C++吧~~
    这个就是域操作符~~
    一般你定义一个类A,然后它有成员变量B或者成员函数F() ~~
    那么访问它们直接用A::B~~A::F()~~~
    就可以访问了~~~
      

  3.   

    ::操作符有三种第一种是全局域操作符,写法:::token它指示编译器的全局范围内查找后面的符号(函数、类、变量等)第二种是域操作系,写法:token::member它指示编译器在token的域内查找member的符号第三种是名空间操作符,与第二种类似,具体参见名空间用法。附MSDN中对该运算符的解释You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator. 
    :: identifier
    class-name :: identifier
    namespace :: identifier
     Res
    The identifier can be a variable or a function.If you have nested local scopes, the scope resolution operator does not provide access to identifiers in the next outermost scope. It provides access to only the global identifiers.Example
    This example has two variables named amount. The first is global and contains the value 123. The second is local to the main function. The scope resolution operator tells the compiler to use the global amount instead of the local one.  Copy Code 
    // expre_ScopeResolutionOperator.cpp
    // compile with: /EHsc
    // Demonstrate scope resolution operator
    #include <iostream>using namespace std;int amount = 123;   // A global variableint main() {
       int amount = 456;   // A local variable
       cout  << ::amount << endl   // Print the global variable
             << amount << endl;    // Print the local variable
    }