Thread: Question about :: operator

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

    Question about :: operator

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

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

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    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
    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
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    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
    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 ( 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
    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.
    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, 06:46 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM