为什么::(作用域)与空左操作数一起使用

Why is :: (scope) used with empty left-hand operand?

本文关键字:操作数 一起 作用域 为什么      更新时间:2023-10-16

我已经看到这种情况好几次了,我一直在挠头想知道为什么…

例如:(http://www.codeguru.com/forum/showthread.php?t=377394)

void LeftClick ( )
{  
  INPUT    Input={0};
  // left down 
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));
  // left up
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}

这个例子没有::(scope)操作符也可以工作,那么为什么还要使用它们呢?

这基本上意味着"获取全局作用域的函数,而不是当前可见的函数"。

void SendInput() { /* (1) */
}
namespace derp {
    void SendInput() { /* (2) */
    }
    void LeftClick() {
        ...
        ::SendInput(); /* matches (1) */
        SendInput();  /* matches (2) */
    }
}

假设您有以下内容:

void bar()
{
}
struct Foo
{
    void bar();
};

如果你想从成员函数Foo::bar中调用全局函数bar,你可以使用左边为空的语法:

void Foo::bar()
{
    // Call the global bar function, not recursively call myself
    ::bar();
}

强制绝对名称解析。
如果没有它,则相对于类/函数命名空间路径搜索名称解析。

假设LeftClick()在命名空间层次结构中:

namespace Level1
{
    namespace Level2
    {
        namespace Level3
        {
            LeftClick()
            {
                 ::SendInput();   // Absolute path only. SendInput in global namespace
                 SendInput();     // Relative path (Note resolved at compile time)
                                  //
                                  // Looks for the function here (in this order)
                                  //    ::Level1::Level2::Level3::SendInput()
                                  //    ::Level1::Level2::SendInput()
                                  //    ::Level1::SendInput()
                                  //    ::SendInput()
            }
        }
    }
}

如果你有一个嵌套的名字就更有趣了:

namespace Level1
{
    namespace Level2
    {
        namespace Level3
        {
            LeftClick()
            {
                 ::Test::Action();  // Absolute Path: Function Action() 
                                    //                in namespace Test 
                                    //                in global namespace
                 Test::Action();    // Relative Path: Function Action()
                                    //                in namespace Test
                                    //                in current namespace path.
                                    //
                     // It will Look for Test (in this order)
                     // ::Level1::Level2::Level3::Test
                     // ::Level1::Level2::Test
                     // ::Level1::Test
                     // ::Test
                     //
                     // In the first Test (and only the first) it finds it will 
                     // try and resolve the Action() function. If it is not there
                     // it is a compile time error.
            }
        }
    }
}

强制在全局范围内查找符号。

void foo() {} // 1
namespace A
{
    void foo() {} // 2
    void bar()
    {
        foo(); // 2
        ::foo(); // 1
    }
}

以这种方式使用作用域操作符意味着您引用的是全局作用域。

为了节省宝贵的时间和按键,请检查没有作用域的作用域解析操作符。

::用于直接从对象外部访问对象