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:
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
function you would write:
std::swap (a, b);
or to use the STL string class you would write
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:
using namespace 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:
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 "
" 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
or by the ambiguous use of int and bool.