14 Replies - 1553 Views - Last Post: 22 December 2010 - 01:42 PM Rate Topic: -----

#1 BerryBoy  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 8
  • Joined: 10-December 10

Does anyone know about meaning of ::

Posted 20 December 2010 - 10:21 PM

*
POPULAR

I'm working with Windows API ( win32 ) in visual C++

I understand that win32 is written in C , After include windows.h you can call it like normal C function.
But in many example in some blog or web site, they use :: before the function name

for example

HANDLE hDir = ::CreateFile(
    strDirectory,
    ...
    NULL);



Is it difference from ?

HANDLE hDir = CreateFile(
    strDirectory,
    ...
    NULL);


This post has been edited by BerryBoy: 20 December 2010 - 10:22 PM


Is This A Good Question/Topic? 5
  • +

Replies To: Does anyone know about meaning of ::

#2 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 231
  • Joined: 07-July 10

Re: Does anyone know about meaning of ::

Posted 20 December 2010 - 10:27 PM

*
POPULAR

It means use global namespace. E.g:

void fn();

namespace ns {
    void fn() {
       ::fn();    // fn in global namespace is called
    }
}

Was This Post Helpful? 10
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Does anyone know about meaning of ::

Posted 20 December 2010 - 11:13 PM

*
POPULAR

And it's called the 'scope resolution operator'.
Was This Post Helpful? 6
  • +
  • -

#4 p_v_a  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 22
  • Joined: 13-December 10

Re: Does anyone know about meaning of ::

Posted 21 December 2010 - 08:09 AM

There are 999998999.1010110 million billion places where you can find out what is the meaning of :: ! Don't you think that if you rely on other people to explain you the simple stuff something is wrong with the way you are approaching this subject?
Was This Post Helpful? -11
  • +
  • -

#5 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Does anyone know about meaning of ::

Posted 21 December 2010 - 08:46 AM

*
POPULAR

@p_v_a: That comment was not necessary. I don't think you can simply google :: and get the answer. I was quite curious with it too. Not everyone have advanced knowledge as you are, some are still learning the basic.

Back to the topic, something that may help(when I google scope resolution operator). http://en.wikipedia....lution_operator
Was This Post Helpful? 6
  • +
  • -

#6 BerryBoy  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 8
  • Joined: 10-December 10

Re: Does anyone know about meaning of ::

Posted 21 December 2010 - 10:11 AM

Thank everyone, sorry to post this simple stuff.

But it's really the first time I heard about 'scope resolution operator'.

So Thankyou.
Was This Post Helpful? 1
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Does anyone know about meaning of ::

Posted 21 December 2010 - 10:19 AM

Don't worry, it's hard to Google symbols. The best thing to go it future would be to actually find a C++ book and check out the index; they usually have references to these and others along with full explanations of their use. :)
Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Does anyone know about meaning of ::

Posted 21 December 2010 - 10:23 AM

In the move to C++ from C you gained the classes. With that gain each class could have a method of the same name. The :: operator was added so you could reference these methods, resolve their scope if you will. Also, don't fret over posting simple questions. We were all beginners at some point.
Was This Post Helpful? 2
  • +
  • -

#9 p_v_a  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 22
  • Joined: 13-December 10

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 02:22 AM

I don't know why you guys think my post was SO unhelpful. I'm just saying that the :: operator is covered in any book about C++ ,including the ones with 7 days/21 days in the title. If you didn't bother reading at least one book about C++ and OOP , WHY would you bother to actually try to write programs or understand something in the code of others.
It's THAT simple, might as well start a topic like "Has anybody figured out what does C++ stand for?" or better still "Does anybody know any other data types than int?"

I'd also like to add that the title of the topic is kind of lame.An on topic answer would be UHM YES, they've taken a book and read it.
Was This Post Helpful? -4
  • +
  • -

#10 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 07:23 AM

@p_v_a: This isn't some elitist site to come to and bash people for asking questions. This is one of the few sites on the internet you can still go to and actually ask a question.
Was This Post Helpful? 1
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 08:30 AM

Have you ever tried to Google an operator? Without knowing some context it is difficult to know what to search for at all. Unfortunately you can't grep dead trees so it can be hard to find in a book.

Even knowing what the scope resolution operator is, it can be difficult to know if-when one needs to use it without a class or namespace in front of it. For example no one has really answered the OP's initial question of, why put :: before windows API calls? which is a very good question that one probably will not find in your general C++ book.

Basically the practice seems to stem from MFC which tends to reuse names of windows API functions which are held in a C header (with no namespace -- therefore global namespace). When you use the API version of the function you add a global scope operator to the beginning of the name to avoid ambiguities. This makes it explicit which function you mean to call. So the convention inside of MFC code has always been to call windows API function as ::function() since as a wrapper of the windows API they often use the same names.

From there, monkey see monkey do, windows code monkeys have been adding the scope operator even when it is technically unnecessary because that is how MSDN does it and it really does make things pretty explicit.

Do you need to add :: before windows API calls? Only if one of your namespaces or the current class uses the function name - otherwise it should not be in scope. But as a general practice, if you may/maynot have some namespaces/classes using the same name as an API call (say because you were wrapping the API into a framework) then it is just better to err on the side of caution and always use the global namespace identifier to mark API functions.

#include <iostream>

void myFunction() { std::cout << "myFunction" << std::endl; }

namespace foo {
    void myFunction() { std::cout << "foo::myFunction" << std::endl; }
    
    namespace bar {
        void doIt() { 
            //Here this is no ambiguity. Scrolling up the namespaces the first function named myFunction()
            //   we find is foo::myFunction() -- that that is the one called
            myFunction();
            
            //if I had wanted the global one I needed to add ::
            ::myFunction();
        }
    }

}


using namespace foo;

int main() {
    //myFunction(); // Error Ambigous call because we have included foo and the global namespace
    
    //so we need to explicity state which function is which
    
    foo::myFunction();
    
    ::myFunction(); 

    foo::bar::doIt();

    return 0;
}

Was This Post Helpful? 3
  • +
  • -

#12 p_v_a  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 22
  • Joined: 13-December 10

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 09:23 AM

View PostCodeGrappler, on 22 December 2010 - 06:23 AM, said:

@p_v_a: This isn't some elitist site to come to and bash people for asking questions. This is one of the few sites on the internet you can still go to and actually ask a question.



Mkay.
It's not my intention to bash anybody.
I guess I have a different interpretation of the forum rules regarding this topic, but if NickDMax thinks the question is legit , no problem and sorry if somebody felt offended.
Was This Post Helpful? 1
  • +
  • -

#13 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 09:38 AM

View PostNickDMax, on 22 December 2010 - 07:30 AM, said:

Even knowing what the scope resolution operator is, it can be difficult to know if-when one needs to use it without a class or namespace in front of it. For example no one has really answered the OP's initial question of, why put :: before windows API calls? which is a very good question that one probably will not find in your general C++ book.


Agreed, I only stumbled upon the answer by complete accident one day while searching for something entirely unrelated. I've never seen scope-resolution explained without a namespace or a class in front of it in any book.

Should have answered the question but alas I did not.
Was This Post Helpful? 0
  • +
  • -

#14 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 12:34 PM

View Postp_v_a, on 22 December 2010 - 08:23 AM, said:

View PostCodeGrappler, on 22 December 2010 - 06:23 AM, said:

@p_v_a: This isn't some elitist site to come to and bash people for asking questions. This is one of the few sites on the internet you can still go to and actually ask a question.



Mkay.
It's not my intention to bash anybody.
I guess I have a different interpretation of the forum rules regarding this topic, but if NickDMax thinks the question is legit , no problem and sorry if somebody felt offended.


I'm just gonna go ahead and throw this one out there, but if you had known the answer to the original question you would have undoubtedly answered it for him instead of flaming. Instead you continued to go on an arrogant rant of which you probably had no clue of yourself or you would have done a more passive way of being a less than constructive person and used lmgtfy to put your point across from google. And if you tried to google the Scope-resolution op like I just did out of curiosity you would get -> what is ::

So basically, when someone is doing a :helpsmilie: don't be a :censored: and instead use the best of your knowledge to assist in the answer, and if you have nothing to say than don't :nono:

Either way, post is off topic and plentiful in the rant.
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Does anyone know about meaning of ::

Posted 22 December 2010 - 01:42 PM

ok. p_v_a has acknowledged his overstep. To be honest people often paste "LMGTFY" searches or jump on members for not doing rudimentary research on a subject before asking a simple question. We all know that if you give a man a fish you feed him tonight, but if you teach him how to fish he will go over to the next town to beg for fish there. So when some guy comes begging for fish we try to show him how to google fish so he will go away. Sometimes we get a little callus and hammer everyone who asks questions about fish.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1