Object declaration with preceding :: in C++ [duplicate] - c++

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.

Related

What is the meaning of multiple class methods separated by "::"? [duplicate]

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.

Can I use the static keyword for classes and structs in C++? [duplicate]

This question already has answers here:
Superiority of unnamed namespace over static?
(2 answers)
Closed 2 years ago.
I understand that you can use the static keyword for functions and variables to prevent them from being visible from other files. Can I use it for classes/structs? For example, inside a function, I use a struct that only that function uses, but I don't want to be able to use it from outside that file and I don't return that struct.
Can I use the static keyword for classes and structs in C++?
No. However, you can define classes in an anonymous namespace which will prevent it from being named in other translation units.

C++ "::" without class name [duplicate]

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.

what does "::functionName()" imply in c++? [duplicate]

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.)

Not using parentheses in constructor call with new (c++) [duplicate]

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).