Question about :: operator

This is a discussion on Question about :: operator within the C++ Programming forums, part of the General Programming Boards category; whats the difference between: Code: return ::CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam); and Code: return CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam); Also, I've seen ...

  1. #1
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50

    Question about :: operator

    whats the difference between:
    Code:
    return ::CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam);
    and
    Code:
    return CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam);


    Also, I've seen source which calls a class member function by
    Code:
    object::function();
    How is it doing this, since I call functions like:
    Code:
    object.function();
    I think I read about that long ago, I'm not too experienced with c++ classes however.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    It's the scope operator. In the first example, the difference is that the first one will always look in the global namespace. The second will look in whatever current namespace it's in plus the global namespace. So if you a function named A in the global namespace and in namespace B and you happen to be defining a function inside namespace B and you to use the global function A, you prefix it with :: to use the global one.

    The second syntax depends on what object and function is. The :: operator can be used to access members of a namespace. Any static members inside a class are said to be part of the class's namespace. Thus, to access them, you would type the class's name followed by :: and the name of whatever you want to access.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Namespace isn't the proper term here. It simply defines a identifier scope.

    But the other parts are right. :: is used to access static methods, while . is used to access regular "instance" methods.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Quote Originally Posted by King Mir View Post
    Namespace isn't the proper term here. It simply defines a identifier scope.
    A namespace is an alternative name for an identifier scope.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've heard some people say that ::function() means run function() from the previous namespace (ex. for nested namespaces) and others say it means the global namespace.

    From my tests (with VC++ 2008) it means the global namespace, but if anyone has a copy of the Standard, can you see which is true?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why yes, I can!




    Oh, you probably wanted to know what the answer was.
    Quote Originally Posted by C++ Standard, clause 7.3.3, paragraph 7
    Members declared by a using-declaration can be referred to by explicit qualification just like other member
    names (3.4.3.2). In a using-declaration, a prefix :: refers to the global namespace.
    There's also this, in the context of expressions instead of using declarations:
    Quote Originally Posted by C++ Standard, clause 5.1, paragraph 4
    The operator :: followed by an identifier, a qualified-id, or an operator-function-id is a primaryexpression.
    Its type is specified by the declaration of the identifier, qualified-id, or operator-function-id.
    The result is the entity denoted by the identifier, qualified-id, or operator-function-id. The result is an
    lvalue if the entity is a function or variable. The identifier, qualified-id, or operator-function-id shall have
    global namespace scope or be visible in global scope because of a using-directive (7.3.4). [Note: the use of
    :: allows a type, an object, a function, an enumerator, or a namespace declared in the global namespace to
    be referred to even if its identifier has been hidden (3.4.3). ]
    Last edited by tabstop; 04-04-2010 at 08:52 PM.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    Quote Originally Posted by grumpy View Post
    A namespace is an alternative name for an identifier scope.
    Hmm, no. A class defines a class scope. A namespace defines a namespace scope. A namespace declarative region differs from a class declarative region in that it can be unnamed and it can span across several translation units.

    Quote Originally Posted by ISO/IEC 14882:2003(E), §7.3.(1), page 114
    A namespace is an optionally-named declarative region. The name of a namespace can be used to access entities declared in that namespace; that is, the members of the namespace. Unlike other declarative regions, the definition of a namespace can be split over several parts of one or more translation units.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new operator question
    By abachler in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2008, 02:06 PM
  2. Question about operator overloading
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 01:02 PM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  4. quick c++ operator question
    By pkananen in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 05:46 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21