Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Do you put :: in front of every Windows API call ?

Do you put :: in front of every Windows API call ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonquestion
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Defenestration
    wrote on last edited by
    #1

    When programming in C++, do you always put :: in front of every Windows API call to indicate it's in the global namespace, or you you think this makes the code ugly ?

    _ R S 3 Replies Last reply
    0
    • D Defenestration

      When programming in C++, do you always put :: in front of every Windows API call to indicate it's in the global namespace, or you you think this makes the code ugly ?

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      Defenestration wrote:

      do you always put :: in front of every Windows API

      No.

      Defenestration wrote:

      indicate it's in the global namespace, or you you think this makes the code ugly ?

      But why the hell do you want to use the API's in Global Namespace? Is there some specific API that you are talking about?

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      D 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        Defenestration wrote:

        do you always put :: in front of every Windows API

        No.

        Defenestration wrote:

        indicate it's in the global namespace, or you you think this makes the code ugly ?

        But why the hell do you want to use the API's in Global Namespace? Is there some specific API that you are talking about?

        You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

        D Offline
        D Offline
        Defenestration
        wrote on last edited by
        #3

        _AnShUmAn_ wrote:

        Is there some specific API that you are talking about?

        Any of the Windows API functions.

        _AnShUmAn_ wrote:

        But why the hell do you want to use the API's in Global Namespace?

        Suppose you have a class which has a member function with the same signature as one of the Windows API functions. If you call this function, without the :: in front, from within one of the class member functions, it will call the class member function instead of the Windows API function. Putting :: in front, causes the Windows API function to be called. So, unless you mean to call the member function, you should really put :: in front of all Windows API functions to ensure they get called. Admittedly, it's unlikely you'll create a member function with the same name and signature as a Windows API function, but it's possible.

        _ 1 Reply Last reply
        0
        • D Defenestration

          _AnShUmAn_ wrote:

          Is there some specific API that you are talking about?

          Any of the Windows API functions.

          _AnShUmAn_ wrote:

          But why the hell do you want to use the API's in Global Namespace?

          Suppose you have a class which has a member function with the same signature as one of the Windows API functions. If you call this function, without the :: in front, from within one of the class member functions, it will call the class member function instead of the Windows API function. Putting :: in front, causes the Windows API function to be called. So, unless you mean to call the member function, you should really put :: in front of all Windows API functions to ensure they get called. Admittedly, it's unlikely you'll create a member function with the same name and signature as a Windows API function, but it's possible.

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          Defenestration wrote:

          So, unless you mean to call the member function, you should really put :: in front of all Windows API functions to ensure they get called.

          Yes, other wise the compiler won't be able to figure out which function do you want to call.

          Defenestration wrote:

          Admittedly, it's unlikely you'll create a member function with the same name and signature as a Windows API function, but it's possible.

          Yes. But then you can certainly change the member function name to something else by prefixing or suffixing it.

          You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

          D 1 Reply Last reply
          0
          • _ _AnsHUMAN_

            Defenestration wrote:

            So, unless you mean to call the member function, you should really put :: in front of all Windows API functions to ensure they get called.

            Yes, other wise the compiler won't be able to figure out which function do you want to call.

            Defenestration wrote:

            Admittedly, it's unlikely you'll create a member function with the same name and signature as a Windows API function, but it's possible.

            Yes. But then you can certainly change the member function name to something else by prefixing or suffixing it.

            You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

            D Offline
            D Offline
            Defenestration
            wrote on last edited by
            #5

            The problem with not putting the :: in front of Win API's when calling them is that you could introduce a subtle bug; the code would compile OK with you thinking your code was calling the Win API, but instead would actually be calling the member function by mistake.

            _ 1 Reply Last reply
            0
            • D Defenestration

              The problem with not putting the :: in front of Win API's when calling them is that you could introduce a subtle bug; the code would compile OK with you thinking your code was calling the Win API, but instead would actually be calling the member function by mistake.

              _ Offline
              _ Offline
              _AnsHUMAN_
              wrote on last edited by
              #6

              Defenestration wrote:

              with you thinking your code was calling the Win API

              no, a developer should take care which version should be called, the global one or the other one in the class and should use :: accordingly

              You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

              D 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                Defenestration wrote:

                with you thinking your code was calling the Win API

                no, a developer should take care which version should be called, the global one or the other one in the class and should use :: accordingly

                You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                D Offline
                D Offline
                Defenestration
                wrote on last edited by
                #7

                Thanks for helping me to think it through. In summary then: :: only needs to be placed in front of Win API functions when they are called from within class member functions, otherwise it's not necessary (ie. when calling a Win API function outside of a class).

                modified on Tuesday, December 9, 2008 2:41 AM

                R 1 Reply Last reply
                0
                • D Defenestration

                  Thanks for helping me to think it through. In summary then: :: only needs to be placed in front of Win API functions when they are called from within class member functions, otherwise it's not necessary (ie. when calling a Win API function outside of a class).

                  modified on Tuesday, December 9, 2008 2:41 AM

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  If you need to call the function declared in global namespace, then use the scope resolution operator, suffixing the API. If you don't do this, the local version of the API will be called. For example, when you write an MFC app, something like MessageBox is provided to you by both - the MFC application framework and is as well available in the global namespace. A call to MessageBox will end up calling the one provided by the application framework, whereas **::**MessageBox will call the one in the global namespace. There's nothing special to it, just used to specify the scope of the API being called.

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  1 Reply Last reply
                  0
                  • D Defenestration

                    When programming in C++, do you always put :: in front of every Windows API call to indicate it's in the global namespace, or you you think this makes the code ugly ?

                    R Offline
                    R Offline
                    Roger Stoltz
                    wrote on last edited by
                    #9

                    Defenestration wrote:

                    When programming in C++, do you always put :: in front of every Windows API call

                    Yes.

                    "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                    "High speed never compensates for wrong direction!" - unknown

                    D 1 Reply Last reply
                    0
                    • D Defenestration

                      When programming in C++, do you always put :: in front of every Windows API call to indicate it's in the global namespace, or you you think this makes the code ugly ?

                      S Offline
                      S Offline
                      Snorri Kristjansson
                      wrote on last edited by
                      #10

                      Yes, but only on mondays

                      1 Reply Last reply
                      0
                      • R Roger Stoltz

                        Defenestration wrote:

                        When programming in C++, do you always put :: in front of every Windows API call

                        Yes.

                        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                        "High speed never compensates for wrong direction!" - unknown

                        D Offline
                        D Offline
                        Defenestration
                        wrote on last edited by
                        #11

                        How about C and C++ standard library function calls ?

                        R 1 Reply Last reply
                        0
                        • D Defenestration

                          How about C and C++ standard library function calls ?

                          R Offline
                          R Offline
                          Roger Stoltz
                          wrote on last edited by
                          #12

                          The scope resolution operator is C++ specific, thus you cannot use it in C.

                          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                          "High speed never compensates for wrong direction!" - unknown

                          D 1 Reply Last reply
                          0
                          • R Roger Stoltz

                            The scope resolution operator is C++ specific, thus you cannot use it in C.

                            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                            "High speed never compensates for wrong direction!" - unknown

                            D Offline
                            D Offline
                            Defenestration
                            wrote on last edited by
                            #13

                            But you can call C library functions from C++ programs. So in this case, would you prefix C library function calls with :: ?

                            R 1 Reply Last reply
                            0
                            • D Defenestration

                              But you can call C library functions from C++ programs. So in this case, would you prefix C library function calls with :: ?

                              R Offline
                              R Offline
                              Roger Stoltz
                              wrote on last edited by
                              #14

                              Defenestration wrote:

                              So in this case, would you prefix C library function calls with :: ?

                              My point is you can't! Try prefixing a call to e.g. strlen() with the scope resolution operator. You'll get a compiler error.

                              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                              "High speed never compensates for wrong direction!" - unknown

                              D 1 Reply Last reply
                              0
                              • R Roger Stoltz

                                Defenestration wrote:

                                So in this case, would you prefix C library function calls with :: ?

                                My point is you can't! Try prefixing a call to e.g. strlen() with the scope resolution operator. You'll get a compiler error.

                                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                                "High speed never compensates for wrong direction!" - unknown

                                D Offline
                                D Offline
                                Defenestration
                                wrote on last edited by
                                #15

                                Roger Stoltz wrote:

                                My point is you can't! Try prefixing a call to e.g. strlen() with the scope resolution operator. You'll get a compiler error.

                                Not if the source filename extension is .cpp

                                modified on Tuesday, December 9, 2008 5:18 AM

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups