CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Location
    New York
    Posts
    154

    When shoud "::mFunc()" syntax be used?

    Hello,

    Assuming we have an object "spb", which has a member function named "getsize".

    Now I want to invoke the "getsize" function. Could some one explain to me the difference between the following 2 methods:

    -------method 1------------
    ::getsize();

    -------method 2------------
    spb test;
    test.getsize();
    -------------------------------

    How are the two methods used under different circumstances ?
    Thanks very much in advance,
    Larry

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Well, ::getsize() is a global function that seemingly operates on
    nothing. test.getsize() is going to call spb's getsize() for the test
    object. They're two totally different concepts.

    I don't know what your global "getsize()" function does and I
    don't know what your "spb::getsize()" function does.

    --Paul

  3. #3
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Oh, I understnad your question now [after reading its subject
    again].
    Basically, you prepend a function name with :: when you want to
    call the GLOBAL version of that function.

    If you're in a class that has a member function getsize(), and you
    type getsize(), you're going to be calling the getsize() member
    function. To call the global version, you prepend the function
    with the scope-resolution operator [::].

  4. #4
    Join Date
    Sep 2002
    Location
    New York
    Posts
    154
    It sounds like you have to declare "getsize" as a global function outside of any object in order to use "::getsize()" syntax?
    Larry

  5. #5
    Join Date
    Sep 2002
    Location
    New York
    Posts
    154
    Thanks very much, it's clear now.
    Larry

  6. #6
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Scope resolution operator

    Yes indeed,

    The double colon operator is called the scope resolution operator which is used to resolve namespace and certain class/structure elements. The simple double colon prefix is the scope resolution operator applied on global scale. Static functions not belonging to any class or namespace can be resolved with this.

    The sample below explains all this a bit more clearly.

    Chris.



    Code:
    class c
    {
    public:
    
      static void nothing(void) { }
    
    };
    
    void nothing(void)
    {
    }
    
    namespace notta
    {
      void nothing(void)
      {
      }
    }
    
    int main(int argc, char* argv[])
    {
      // Global function nothing()
      ::nothing();
      
      // Member function nothing()
      ::c::nothing();
    
      // Namespace function nothing()
      ::notta::nothing();
    
      return 1;
    }
    You're gonna go blind staring into that box all day.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured