Why does C++ need the scope resolution operator?
Why C++ doesn't use .
where it uses ::
, is because this is how the language is defined. One plausible reason could be, to refer to the global namespace using the syntax ::a
as shown below:
int a = 10;
namespace M
{
int a = 20;
namespace N
{
int a = 30;
void f()
{
int x = a; //a refers to the name inside N, same as M::N::a
int y = M::a; //M::a refers to the name inside M
int z = ::a; //::a refers to the name in the global namespace
std::cout<< x <<","<< y <<","<< z <<std::endl; //30,20,10
}
}
}
Online Demo
I don't know how Java solves this. I don't even know if in Java there is global namespace. In C#, you refer to global name using the syntax global::a
, which means even C# has ::
operator.
but I can't think of any situation in which syntax like this would be legal anyway.
Who said syntax like a.b::c
is not legal?
Consider these classes:
struct A
{
void f() { std::cout << "A::f()" << std::endl; }
};
struct B : A
{
void f(int) { std::cout << "B::f(int)" << std::endl; }
};
Now see this (ideone):
B b;
b.f(10); //ok
b.f(); //error - as the function is hidden
b.f()
cannot be called like that, as the function is hidden, and the GCC gives this error message:
error: no matching function for call to ‘B::f()’
In order to call b.f()
(or rather A::f()
), you need scope resolution operator:
b.A::f(); //ok - explicitly selecting the hidden function using scope resolution
Demo at ideone
Why do you use a Scope Resolution Operator when defining a class' method?
When you define a class:
struct foo {
void bar() {}
};
Then the full name of bar
is ::foo::bar
. The leading ::
to refer to the global namespace can often be omitted. There is no bar
in the global namespace, hence bar
alone (or ::bar
) does not name an entity and when you define the method out of line you need to tell what bar
you mean:
struct foo {
void bar();
};
struct baz {
void bar();
};
void bar() {} // this defines a completely unrelated free function called bar
void foo::bar() {} // defines foo::bar
void baz::bar() {} // defines baz::bar
You need the scope resolution operator to state which method you want to define.
For more details I refer you to https://en.cppreference.com/w/cpp/language/lookup
What's the purpose of scope resolution operator when using ios_base in C++
A general way to view the scope resolution operator is to say that you use it for resolving things that can be resolved statically. This includes the things that you listed in the question, but other things should be included as well.
Most notably, your list does not include static members of a class. This is precisely what in
and out
are - they are static data members, so you need scope resolution operator to resolve them. The applicability is not limited to static data members: static member functions are also resolved using the scope resolution operator.
why constructors need scope resolution to define it outside the body?
In the early days of C, you didn't have to explicitly specify a return type for a function. If you didn't, it was assumed to be int
. When C++ evolved, it needed to be backwards compatible with C, and there were already many C programs which relied on this behavior to some extent. For many years, even though not specifying a return type had become an error both in C and in C++, compilers still only generated warnings for it, probably to avoid breaking existing code.
Hence, at that time, writing something like
MyClass() { /* ... */ }
might have been interpreted as a function int MyClass(void)
. And if you wrote a class, you didn't want to conflict with that function.
Also, since the constructor is a function, after all, it makes sense to treat all member function definitions consistently.
when do we use scope resolution operator before new ( ::new)?
::new
is the explicit global operator. This is as opposed to the various class-scoped operators new
which may be defined. For example, if I define an operator new
inside myclass
, and then in that same class I want to use the global one, I would say ::new
, whereas if I said new
I would get the class-specific function I defined.
I might also use ::new
in generic template code where I am not sure what type I might be allocating, but want to make sure I do not use any class-specific allocator (for example I might need to pass the result to some API which will use global ::delete
on it).
Here's a big list of all possible operators new
for reference: http://en.cppreference.com/w/cpp/memory/new/operator_new
unable to understand use of scope resolution operator
It's pretty straigtforward, I guess you're not familiar with namespaces.
::mediapipe::Status RunMPPGraph()
RnuMMPGraph
is a function taking zero arguments and returning ::mediapipe::Status
. Status
is a type defined in the mediapipe
namespace which is defined in the global namespace.
Why is `::` called the 'scope resolution operator' when it doesn't act like an operator?
But it is an operator, as much as say the member selection operator .
:
#include <iostream>
int n;
int main()
{
int n = 1;
std::cout << ::n << " " << n;
}
and
#include <iostream>
struct N {
int n = 1;
operator int() const {return 0;}
};
int main()
{
N n;
std::cout << n << " " << n.n;
}
The output is the same in both cases.
Related Topics
Get Address Family from Socket. Linux
/Usr/Lib64/Libstdc++.So.6: Version 'Glibcxx_3.4.15' Not Found
While (1) VS. for (;;) Is There a Speed Difference
How to Detect the Number of Physical Processors/Cores on Windows, MAC and Linux
C++ - Enum VS. Const VS. #Define
How to Read/Write Arbitrary Bits in C/C++
How to Use C++ Classes with Ctypes
How to Solve "Unresolved Inclusion: <Iostream>" in a C++ File in Eclipse Cdt
Replacement for Deprecated Register Keyword C++ 11
What Are the Rules for the "..." Token in the Context of Variadic Templates
Why Is Padding Added for Multiple Data Members of Structures and Not for Single Members
How to Make Std::Vector's Operator[] Compile Doing Bounds Checking in Debug But Not in Release
In Which Order Should Floats Be Added to Get the Most Precise Result
Send and Receive a File in Socket Programming in Linux with C/C++ (Gcc/G++)