Understanding A::A() : _b(XXX,XXX) in C++
-
A::A() : _b(XXX,XXX); The colon denotes member initialization, meaning the values XXX, XXX are passed to initialize the member variable _b of type B.
-
This is called a member initializer list.
-
_b is a member variable of class A.
During the construction of A, the variable _b is initialized.
You should review the syntax of constructors to better understand this. -
This is initialization; for example, constants must be initialized this way.
-
A() indicates the constructor of class A, specifying its scope; : _b(XXX,XXX); refers to initializing the member variable.
===============================================================================================
C++ Scope Resolution Operator (::) Explained (Reprinted)
http://blog.csdn.net/lychee007/article/details/5386039
First, here are explanations from various sources — their meanings are essentially the same.
Visual C++ Language Reference
Scope Resolution Operator: ::
You can tell the compiler to use the global identifier instead of the local one by prefixing the identifier with ::, the scope resolution operator.
- :: identifier
- class-name :: identifier
- namespace :: identifier
Remarks The identifier can be either a variable or a function.
If there are nested local scopes, the scope resolution operator does not grant access to identifiers in the immediately outer scope. It only provides access to global identifiers.
Example This example declares two variables named amount. The first is global and holds 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.
-
// expre_ScopeResolutionOperator.cpp
-
// compile with: /EHsc
-
// Demonstrate scope resolution operator
-
#include
-
using namespace std;
-
int amount = 123; // A global variable
-
int main() {
-
int amount = 456; // A local variable
-
cout << ::amount << endl // Print the global variable
-
<< amount << endl; // Print the local variable
-
}
IBM XL C/C++ for AIX, V10.1
Scope resolution operator :: (C++ only)
The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator when a name in namespace or global scope is hidden by a declaration of the same name within a block or class. For example:
-
int count = 0;
-
int main(void)
-
{
-
int count = 0;
-
::count = 1; // set global count to 1
-
count = 2; // set local count to 2
-
return 0;
-
}
The declaration of count inside the main function hides the global variable count. The statement ::count = 1 accesses the global variable named count.
You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can access it by qualifying it with its class name and the scope resolution operator.
In the following example, the declaration of the variable X hides the class type X, but you can still access the static class member count by qualifying it with the class name X and the scope resolution operator.
-
#include
-
using namespace std;
-
class X
-
{
-
public:
-
static int count;
-
};
-
int X::count = 10; // define static data member
-
int main ()
-
{
-
int X = 0; // hides class type X
-
cout << X::count << endl; // use static member of class X
-
}
Sun Studio 12: dbx
C++ Scope Resolution Operator (::)
The double colon operator (::) can be used to qualify C++ member functions, top-level functions, or variables with global scope in the following cases:
-
Overloaded names (same name used with different parameter types)
-
Ambiguous names (same name used in different classes)
Wiki:
-
#include
-
using namespace std;
-
int n = 12; // A global variable
-
int main()
-
{
-
int n = 13; // A local variable
-
cout << ::n << endl; // Print the global variable: 12
-
cout << n << endl; // Print the local variable: 13
-
}