C++ scope-resolution double-colon operator :: and dot .

7 views
Skip to first unread message

James Harris

unread,
Aug 22, 2021, 7:52:00 AMAug 22
to
Following other discussions here I find myself having to read C++ code
and, frankly, I know very little about it so I have a basic question:

What does the :: operator mean and could be replaced - in a better
language - with a dot?

As I have it,

A.B

refers to B as a name within A. That form is perhaps most familiar as
identifying a field in a record but I currently have it as picking any
name from any namespace. So what would C++'s double colon add to that?


--
James Harris

Bart

unread,
Aug 22, 2021, 9:30:52 AMAug 22
to
On 22/08/2021 12:51, James Harris wrote:
> Following other discussions here I find myself having to read C++ code
> and, frankly, I know very little about it so I have a basic question:
>
> What does the :: operator mean and could be replaced - in a better
> language - with a dot?

Short answer: yes.

> As I have it,
>
>   A.B
>
> refers to B as a name within A. That form is perhaps most familiar as
> identifying a field in a record but I currently have it as picking any
> name from any namespace. So what would C++'s double colon add to that?


Nothing, except telling you that A::B is doing scope resolution rather
member selection. With A.B, either thing could be going on.

But I've also seen :: used by itself as in ::B. Not sure what that
means, I guess some default (std?) is assumed to go before the ::.


David Brown

unread,
Aug 22, 2021, 10:08:55 AMAug 22
to
On 22/08/2021 15:30, Bart wrote:
> On 22/08/2021 12:51, James Harris wrote:
>> Following other discussions here I find myself having to read C++ code
>> and, frankly, I know very little about it so I have a basic question:
>>
>> What does the :: operator mean and could be replaced - in a better
>> language - with a dot?
>
> Short answer: yes.
>

Agreed, for the short answer.

Longer answer: it could be replaced by a dot, but that would not
necessarily be better. There are clarity advantages to have more than
one scoping operator to distinguish between different uses.

>> As I have it,
>>
>>    A.B
>>
>> refers to B as a name within A. That form is perhaps most familiar as
>> identifying a field in a record but I currently have it as picking any
>> name from any namespace. So what would C++'s double colon add to that?
>
>
> Nothing, except telling you that A::B is doing scope resolution rather
> member selection. With A.B, either thing could be going on.

Exactly.

>
> But I've also seen :: used by itself as in ::B. Not sure what that
> means, I guess some default (std?) is assumed to go before the ::.
>

If you write something like :

namespace A {
int x;
}

namespace B {
namespace A {
int y;
}
}

using namespace B;

int z = A::x;

then the compiler will complain about the ambiguity - it can see two
completely different namespaces called "A" (the global one, and the one
pulled into the global space by "using namespace B;"). But you can write:

int z = ::A::x;

to skip the "using namespace" shortcuts and go back up to the root of
the namespaces.

There are a few people who insist on writing "::std::" instead of
"std::" in C++, through some misguided belief that it makes their code
safer or more robust in the face of some C++ programmer defining their
own "std" namespace. Most C++ programmers consider it totally
unnecessary and a bad habit.

But there can be other occasions with complicated namespace setups where
a global scope namespace initial "::" is helpful.




Reply all
Reply to author
Forward
0 new messages