2011-12-30 19 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. 
      } 
     } 
    } 
} 
相關問題