Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    The use of :: and what does it mean.

    I've seen calls that just begin with :: for example...

    ::MessageBox(NULL, "Error!", "Error", MB_OK | MB_ICONWARNING);

    but it works fine like this too.

    MessageBox(NULL, "Error!", "Error", MB_OK | MB_ICONWARNING);


    What is the reason for :: ?

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    I assume you mean c++, it's a way of defining the namespace you want to use:
    Code:
    #include <iostream>
    using namespace std;
    int main () {
    cout<<"Test";
    return 0;
    }
    OR
    Code:
    #include <iostream>
    int main () {
    std::cout<<"Test";
    return 0;
    }
    Both of these are valid, and the second is useful if you want to use a specific namespace for a specific piece of code. It's actually the bit before and after the "::" that's important.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    More precisely it is the scope operator. You use it to reference items in a different scope. Like if you had a class called 'A' that had a public class inside of it called 'B' and you wanted to instantiate B from outside of A, you would have to do:

    Code:
    A::B myClass;
    OracleGuy

  • #4
    New Coder
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks guys. That helps me understand a bit of what's going on.

    I understand scope and namespaces. So when :: is used in the correct way, it makes sense like...

    std::string myStr;

    Windows API calls are not encapsulated in a class, struct or namespace. So why someone would write...

    ::MessageBox(NULL, "Error!", "Error", MB_OK | MB_ICONWARNING);

    ... is beyond me. I can call any function with :: in front of it and the compiler doesn't seem to care.

    In this code I'm looking at, the windows API calls begin with :: but doesn't seem necessary.

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by GameCodingNinja View Post
    Thanks guys. That helps me understand a bit of what's going on.

    I understand scope and namespaces. So when :: is used in the correct way, it makes sense like...

    std::string myStr;

    Windows API calls are not encapsulated in a class, struct or namespace. So why someone would write...

    ::MessageBox(NULL, "Error!", "Error", MB_OK | MB_ICONWARNING);

    ... is beyond me. I can call any function with :: in front of it and the compiler doesn't seem to care.

    In this code I'm looking at, the windows API calls begin with :: but doesn't seem necessary.
    It is probably just someone's coding style.

    There can be reasons do to something like that, like if you were in a class that had a member method called MessageBox and you wanted to call the one at the root scope.
    OracleGuy

  • #6
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    I can't see any reason why they would use that, it could be a short hand for std I suppose, written into the compiler?

  • #7
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by satchel View Post
    I can't see any reason why they would, it could be a short hand for std I suppose, written into the compiler?
    No it isn't, like the OP said, the API functions are in the global scope.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •