I have seen many type of this statement ( :: ). I know it use for declaring a class member function out of the class( The left point must be a Class , and the right point must be a function , true? ) . But I have seen many different types of that . I don't know what are those means

3 solutions

Solution 3

Both answers given so far have not fully explained the examples given in the question. Therefore, let me add a third answer here.

The :: is used in C++ as scope separator. In plain English that means, it separates the names of classes, structs, and namespaces. As you have mentioned in your question, you have already understood the role in separating class names and struct names from member names, for example:

int MyClass::MyFunction()
{
...
}

The other common usage is with namespaces. Many libraries declare all their classes and functions inside a namespace to avoid conflicts with other libraries and user code. A well known example is the STL standard library, which encloses all its names in a namespace called "std". So, to use for example STL's swap function you would write:

std::swap (a, b);

or to use the STL string class you would write

std::string myString;

If there is no risk of name conflicts you can make your life a little easier and tell the compiler to also look into namespace "std" whenever it searches for a name by saying:

usingnamespace std;

at the beginning of a source file. Then you can omit the std:: prefix in the abour examples.

Note that namespaces can be nested, so don't be surprised to see:

BigLib::SubSection::SuperSmartClass myClass;

Your last question was unrelated to that subject: What is the default type the compiler assigns in the case of absence of a type definition?

Answer: In the C language that used to type "int" in the old days; but new code should not make use of that. In C++ there is no longer a default type. The compiler will issue an error message if you forget to specify the type -- for a good reason. Many programming errors occurred by simply forgetting about the return type of a function and assuming it was int or by the ambiguous use of int and bool.

Solution 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 if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:

The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.

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 use it by qualifying it with its class name and the class scope operator.

In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.

number is a static method inside QString class where the first parameter (*itrCircles)[2] is the third number of itrCircles array, the second parameter is the format and the last parameter is the precision.