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

发布于 2024-12-23 14:45:18 字数 528 浏览 1 评论 0原文

我已经见过几次了,我一直在挠头想知道为什么......

例如:(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));
}

这个例子无需 ::(作用域)运算符即可工作,那么为什么它们还在那里呢?

I've seen this a few times now, and I've been scratching my head wondering why...

As an example: (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));
}

This example works without the :: (scope) operators so why are they even there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挖个坑埋了你 2024-12-30 14:45:18

这基本上意味着“获取全局范围的函数,而不是当前可见的函数”。

void SendInput() { /* (1) */
}

namespace derp {
    void SendInput() { /* (2) */
    }

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

This basically mean "get the GLOBAL scoped function, instead of the currently visible one".

void SendInput() { /* (1) */
}

namespace derp {
    void SendInput() { /* (2) */
    }

    void LeftClick() {
        ...
        ::SendInput(); /* matches (1) */
        SendInput();  /* matches (2) */
    }
}
黯然 2024-12-30 14:45:18

假设您有以下内容:

void bar()
{
}

struct Foo
{
    void bar();
};

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

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

Lets say you have the following:

void bar()
{
}

struct Foo
{
    void bar();
};

If you want to call the global function bar from the member function Foo::bar you use the syntax with empty left hand side:

void Foo::bar()
{
    // Call the global bar function, not recursively call myself
    ::bar();
}
樱娆 2024-12-30 14:45:18

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

因此,假设 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.
            }
        }
    }
}

It forces an absolute name resolution.
Without it name resolution is searched for relative to the class(s)/functions namespace path.

So assume LeftClick() is in the namespace hierarchy:

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

It becomes more interesting if you have a nested name:

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.
            }
        }
    }
}
浅唱ヾ落雨殇 2024-12-30 14:45:18

这是为了强制在全局范围内查找符号。

void foo() {} // 1

namespace A
{
    void foo() {} // 2

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

It's to force the symbol to be looked up at global scope.

void foo() {} // 1

namespace A
{
    void foo() {} // 2

    void bar()
    {
        foo(); // 2
        ::foo(); // 1
    }
}
暖风昔人 2024-12-30 14:45:18

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

为了节省我宝贵的时间和击键次数,请查看没有作用域的作用域解析运算符

Using the scope operator in this fashion means that you are referring to the global scope.

To save me valuable time and keystrokes, check out scope resolution operator without a scope.

痴意少年 2024-12-30 14:45:18

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

The :: is used to give access to an object directly from outside of the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文