Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7516389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:06:56+00:00 2026-05-30T01:06:56+00:00

(I know what the scope resolution operator does, and how and when to use

  • 0

(I know what the scope resolution operator does, and how and when to use it.)

Why does C++ have the :: operator, instead of using the . operator for this purpose? Java doesn’t have a separate operator, and works fine. Is there some difference between C++ and Java that means C++ requires a separate operator in order to be parsable?

My only guess is that :: is needed for precedence reasons, but I can’t think why it needs to have higher precedence than, say, .. The only situation I can think it would is so that something like

a.b::c;

would be parsed as

a.(b::c);

, but I can’t think of any situation in which syntax like this would be legal anyway.

Maybe it’s just a case of “they do different things, so they might as well look different”. But that doesn’t explain why :: has higher precedence than ..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:06:58+00:00Added an answer on May 30, 2026 at 1:06 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know in C++ variables have block scope, for example, the following code works
What scope does the strict mode pragma have in ECMAScript5? use strict; I'd like
Does anyone know how to add a scope bar to a UITableView ? The
I don't know if this has to do with how FindControl works or how
I know this is a stupid question, but I have been teaching myself c#,
In my background in C++ I was a supporter of using the scope resolution
I know, we can use scope identity, or top 1 with order by column
I now know this works: function outerfunction(arg1, arg2, arg3) { var others; //Some code
I know how the Singleton pattern works, but I have doubt how it works
I know this in general is beyond the scope of SO, but I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.