为什么C ++需要范围解析运算符? [英] Why does C++ need the scope resolution operator?

查看:93
本文介绍了为什么C ++需要范围解析运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我知道范围解析运算符的作用,以及如何以及何时使用它.)

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

为什么C ++具有::运算符,而不是为此目的使用.运算符? Java没有单独的运算符,并且运行良好. C ++和Java之间有什么区别,这意味着C ++需要单独的运算符才能进行解析吗?

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;

将被解析为

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

推荐答案

为什么C ++在使用::的地方不使用.,是因为这是语言的定义方式.一个合理的原因可能是,使用语法::a来引用全局名称空间,如下所示:

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
           }
    }
}

在线演示

我不知道Java如何解决这个问题.我什至不知道在Java中是否有全局名称空间.在C#中,您使用语法global::a引用全局名称,这意味着即使C#也具有::运算符.

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.

谁说像a.b::c这样的语法不合法?

Who said syntax like a.b::c is not legal?

请考虑以下课程:

struct A
{
    void f() { std::cout << "A::f()" << std::endl; }
};

struct B : A
{
    void f(int) { std::cout << "B::f(int)" << std::endl; }
};

现在看到此( ideone ):

B b;
b.f(10); //ok
b.f();   //error - as the function is hidden

b.f()不能这样调用,因为该函数是隐藏的,并且GCC会显示以下错误消息:

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()’

要调用b.f()(或更确切地说是A::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

ideone上的演示

这篇关于为什么C ++需要范围解析运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆