-3

I found a C++ file in PARSEC benchmark suite and saw some functions like this:

long Rng::rand()
{
    return _rng->randInt();
}

what does the :: in the name of the function do here?

devnull
  • 118,548
  • 33
  • 236
  • 227
Mehran Abbasi
  • 103
  • 1
  • 6
  • 4
    That is not C, but C++ – David Rodríguez - dribeas Jun 01 '13 at 07:13
  • 1
    This does indeed look like a C++ method definition rather than a C function. Are you sure it was C code and not C++? – yzt Jun 01 '13 at 07:13
  • 2
    I'm sorely tempted to rollback the edits by [devnull](http://stackoverflow.com/users/2235132/devnull) and [Randy Howard](http://stackoverflow.com/users/2159730/randy-howard) since they completely rewrite the question. The original question was about `::` in C; it has been revised so that C is no longer a tag or mentioned in the question, which makes my answer immaterial (because it addresses the original, C question and not the revised, C++ question). Edits are fine when they keep the intent of the original question. They aren't when they don't. – Jonathan Leffler Jun 01 '13 at 07:56

2 Answers2

10

In C++ :: is the Scope resolution operator.
In this case it tells the compiler that it is a defintiion for rand() method which is a member function for Rng class/structure/union/namespace.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alok Save
  • 202,538
  • 53
  • 430
  • 533
10

In C, :: is a syntax error unless it occurs inside a comment, a character literal or a string literal.

The :: can only appear in C++ code.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • How can `::` appear inside a *character* literal? Am I missing something? – yzt Jun 01 '13 at 07:14
  • @H2CO3 And that isn't a syntax error? Two characters inside single quotes? – yzt Jun 01 '13 at 07:16
  • 2
    @yzt [nope](http://stackoverflow.com/questions/3960954/c-multicharacter-literal) –  Jun 01 '13 at 07:16
  • 1
    @H2CO3 Hmmm... thanks! Something new everyday! – yzt Jun 01 '13 at 07:19
  • 1
    Multi-character character constants have an implementation-defined value, so they are not portable, but they are legitimate. See [Does anyone know of a C compiler that fails to compile this?](http://stackoverflow.com/questions/328215/does-anyone-know-of-a-c-compiler-that-fails-to-compile-this) for an extreme example. – Jonathan Leffler Jun 01 '13 at 07:20
  • Note that character literals/constants in C are actually ints - hence why older functions like getc, fgetc etc return ints, and functions like toupper and putc take ints instead of chars... Win32 programming makes heavy use of it with people giving things like timers and events integer values formed from four-character constants: `mainWnd->StartTimer('BEEP');`. – kfsone Jun 01 '13 at 08:11