2011-12-30 37 views
9

我已经看过这几次现在,我一直在挠我的头想知道为什么...为什么::(范围)与空的左侧操作数一起使用?

作为一个例子:(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)); 
} 

这个例子没有使用::(范围)运算符,所以为什么它们在那里?

+0

它也可以,如果'//左down'是除去。那为什么呢?为了清晰。 – tenfour 2011-12-30 12:32:30

+0

@tenfour不是真的... – 2011-12-30 12:33:28

+0

假设OP是正确的,即使没有'::'也能“工作”,那么是的,真的。但是我知道还有更多,这就是为什么我没有发布答案。 – tenfour 2011-12-30 12:34:34

回答

22

这基本上是指“获取GLOBAL作用域函数,而不是当前可见的函数”。

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

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

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

在这种情况下(它表示全局)。它实际上意味着使用“绝对命名空间路径”而不是“相对命名空间路径”。它恰好是这个环境中的一条短路径,因此也是全局命名空间中的一条短路径。 – 2011-12-30 15:41:35

+0

@LokiAstari你能详细说明一下吗?它与将'/ path/to/file.txt'而不是'./path/to/file作为文件引用基本相同。txt' - 一个是相对于当前位置,另一个是相对于某个特定位置? (在这种情况下是根文件夹) – 2016-02-08 08:58:49

+0

@QPaysTaxes:一个很好的模拟器。但是你也需要应用PATH变量来使模拟更好。将在路径中的每个目录中搜索相对路径(绝对路径不会)。在C++中,PATH由当前位置的每个嵌套范围表示。请参阅https://gist.github.com/Loki-Astari/bddbdc98e8c8b9da5edc这会使相关路径容易被新代码添加到父命名空间中。 – 2016-02-08 17:11:57

1

这是强制在全球范围内查找符号。

void foo() {} // 1 

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

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

比方说你具备以下条件:

void bar() 
{ 
} 

struct Foo 
{ 
    void bar(); 
}; 

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

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

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

2

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

所以假设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. 
      } 
     } 
    } 
} 
相关问题