Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 833
replicant Avatar asked Dec 30 '11 12:12

replicant


People also ask

Why does C++ use double colon?

Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.

What is the use of :: operator?

In C++, scope resolution operator is ::. It is used for following purposes. 2) To define a function outside a class. 3) To access a class's static variables.

What is use of Scope Resolution Operator in C++?

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

Which operator is used to resolve the scope?

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.


1 Answers

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) */
    }
}
like image 132
Giovanni Funchal Avatar answered Oct 13 '22 20:10

Giovanni Funchal