-5

I have code I am going over that looks like this:

foo::foofa(string n){
    loadFoo(fn);
}

What does the foo::foofa mean? I do not quite understand what does :: do? Thanks.

EDIT: Also, is there another way to write this without the :: or is it required?

user12074577
  • 1,371
  • 8
  • 25
  • 30
  • 2
    @NickVaccaro It's hard to google `::` if you don't know it's called *scope resolution operator*. What the OP needs to do is [read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Praetorian Apr 29 '13 at 18:34
  • 1
    @Praetorian I copied and pasted the question title, and the result showed up immediately. – Nick Vaccaro Apr 29 '13 at 18:35
  • 1
    @Nick So it does; I stand corrected! – Praetorian Apr 29 '13 at 18:36

4 Answers4

9

:: is the scope operator to used to identify and specify the context that an identifier refers to.

using a very simple google search, IBM describes it as:

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
8

I do not quite understand what does :: do?

It's the scope resolution operator.

If foo is a class (or a namespace), and foofa is something declared inside that class, then within the class you can refer to it simply as foofa. But outside the class, you need to use this operator to specify that you mean this particular foo::foofa; there could be others scoped inside other classes or namespaces.

Also, is there another way to write this without the :: or is it required?

It's required outside the class definition. You could define the function inside the class:

class foo {
    void foofa(string n) { // No foo:: needed here
        loadFoo(n);
    }
};

If foo is a namespace, then you can also use using to avoid the need for :: but that's often a bad idea, so I won't show you how.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2
::

is the scope resolution operator. Quoted from Scope Resolution Operator

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.

Community
  • 1
  • 1
taocp
  • 23,276
  • 10
  • 49
  • 62
1

:: indicates a scope. So either a namespace or class name. For instance if you want to access the sort function in the standard (std) namespace you would use

std::sort
Joel
  • 5,618
  • 1
  • 20
  • 19