This question already has answers here:
What is the meaning of prepended double colon "::"?
(9 answers)
:: without a namespace
(3 answers)
Why is :: (scope) used with empty left-hand operand? [duplicate]
(6 answers)
Closed 9 years ago.
I came across a piece of code which looks like this:
::GetSystemDirectory(buffer, MAX_PATH);
I've never seen a function call preceded by an empty ::. I've always seen them being used with namespaces.
Can someone please explain me what does an empty :: mean ?
It's the scope resolution operator. With nothing in front of it, it indicates global scope.
So for instance, suppose you have a class that defines its own GetSystemDirectory method. Within the code of a method of that class, to call the global one, you'd need the :: in front of it, otherwise by default you'd get the one specific to the class. (And similarly for namespaces.)
Related
This question already has answers here:
What does the "::" mean in C++?
(4 answers)
Closed last year.
I found this line of code where I'm working, but I don't quite understand its meaning:
virtual method1::method2::method3 f() = 0;
In the main function, I have method1::method2::method3.g1().g2(). I really don't understand. I know method::A where A is a class.
:: is a scope operator.
You may need to append multiple of them either when using nested classes (a class defined within another class) or when you use namespaces. Resolution is done the same way.
:: is the scope resolution operator. It allows you to statically traverse scopes such as namespaces and classes in order to reference the identifier you want.
I think what you have is not methods but namespaces and classes.
This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 4 years ago.
I have a class called "Complex" which is intended to represent complex numbers. I have defined an empty default constructor for the class that only prints a message to the screen.
When I try to create an object of the class in the main function as follows:
Complex c1();
The compiler(I am using BorlandC) doesn't give a syntax error but it doesn't create the object. How does the compiler interpret this line?
When you write this:
int foo();
…it declares a function called foo that returns an int.
When you write this:
Complex c1();
…it declares a function called c1 that returns a Complex.
Lose the ().
Contrary to popular belief, this is not quite "the most vexing parse", but it is close.
This question already has answers here:
What's the purpose of a leading "::" in a C++ method call
(2 answers)
Closed 6 years ago.
What does following represent in C++?
::SomeNameSpace::SomeClass abc;
Basically how is this different from
SomeNameSpace::SomeClass abc;
The :: operator is the scope operator; it denotes some specific scope. If there is no prefix then it denotes the global scope.
So if SomeNameSpace is in the global scope, then ::SomeNameSpace and SomeNameSpace are the same. The difference is if you're in a non-global scope and use SomeNameSpace, because then it's first looked for in the current scope.
This question already has answers here:
scope resolution operator without a scope
(6 answers)
Closed 5 years ago.
I came across the following code structure in C++:
uint32_t AClass::Action(....)
{
..
status = ::Action(...);
..
}
I am not sure what ::Action() means. Which class does it belongs to? NOTE: the argument list of ::Action(...) is different from AClass::Action(...).
The leading :: just means that Action here refers to a non-member function in the global namespace, instead of referring to AClass::Action in the current namespace.
::Action() mean it is a function under the the global namespace.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Do the parentheses after the type name make a difference with new?
So I had in my main:
Class* pC = new Class;
It was working as
Class* pC = new Class();
I realized just today that I had omitted the parentheses (so I was hit by the "opposite" of the most vexing parse in a way).
My question: Are these two forms equivalent ?
If the class has a default constructor defined, then both are equivalent; the object will be created by calling that constructor.
If the class only has an implicit default constructor, then there is a difference. The first will leave any members of POD type uninitialised; the second will value-initialise them (i.e. set them to zero).