10

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

I thought i knew everything but something always seems to pop up. Maybe i am forgetting something. What does the : ::name mean? I suspect ::google means from the global namespace use google, protobuf, message. But what does the : do before it? Theres no text on the left so it cant be a label (or can it?!). So what is it?

Namespace::Namespace()
  : ::google::protobuf::Message() {
  SharedCtor();
}

-edit- I feel silly, the indention got me. I thought i was looking inside the function body. I was so hoping it would be something new.

Community
  • 1
  • 1
  • 4
    Apparently so, and it seems to be derived from ::google::protobuf::Message. – EboMike Jan 17 '11 at 05:00
  • 7
    If you don't know about such important basic language feature as *constructor initializer list*, then you are still very far from "knowing everything". – AnT stands with Russia Jan 17 '11 at 05:12
  • 15
    Once you know everything about C++ you are about half way there. Once you realize you were only half way there and learn the second half you learn that was only half of what you actually did not know. Repeat... – Martin York Jan 17 '11 at 05:17
  • @Martin: Which means we can only get asymptotically closer to knowing everything about C++, we can never actually get there. Right? 8v) – Fred Larson Jan 17 '11 at 05:35
  • @AndreyT, @Martin York: Crap! the indention got me. I thought that part was INSIDE a function, thus the label comment! i feel silly and want to -1 myself (but no one do it, many multiples would occur) –  Jan 17 '11 at 05:36
  • @Fred Larson: Infinite series is a real cool subject. You can actually sum them up. In this case the sum of 1/2+1/4+1/8+1/16+1/32.... does actually sum to 1 but only after infinite steps. So assuming each step has a constant time factor you will not do it in your lifetime. BUT yes my assertion is that C++ is too big/complex to know everything, I always find something new (or get corrected about something I thought was true and have to go back and read more about it). – Martin York Jan 17 '11 at 15:07
  • @LokiAstari: add to that that is a moving target, both because of newer standards and of vendor extensions... – user1284631 Jul 16 '13 at 08:24

4 Answers4

18

In a constructor, using a colon is used for variable initialization and/or calling the parent constructor. For example:

struct foo {
   foo();
   foo(int var);
};

struct bar : public foo {
   bar();
   int x;
};

Now you could do bar's constructor like this:

bar::bar()
: x(5)
{
}

That sets x to 5. Or:

bar::bar()
: foo(8)
, x(3)
{
}

That uses foo's second constructor with 8 as an argument, then sets x to 3.

It just looks funny in your code since you have the combination of : for the initialization and :: to get to global namespace.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • I feel silly, i thought i was looking inside the function body. +1 and accepted. –  Jan 17 '11 at 05:38
  • 2
    No reason to feel silly. I've seen lots of things that made me do a double-take at first. Some programming languages can really mess with your mind :) – EboMike Jan 17 '11 at 06:53
  • thanks a lot EboMike for complete and clear comment. – Chavoosh Apr 20 '14 at 16:50
12

The first colon : is actually there as an indication that what follows is an initializer list. This can appear in a class's constructor as a way to give that class's data members some initial value (hence the name) before the body of the constructor actually executes.

A small example, formatted differently:

class Foo {
public:
    Foo() :
        x(3),       // int initialized to 3
        str("Oi!"), // std::string initialized to be the string, "Oi!"
        list(10)    // std::vector<float> initialized with 10 values
    { /* constructor body */ }

private:
    int x;
    std::string str;
    std::vector<float> list;
};

EDIT

As an additional note, if you have a class that subclasses another, the way you invoke your superclass constructor is exactly like this, inside the initializer list. However, instead of specifying the name of a member, you specify the name of the superclass.

Dawson
  • 2,673
  • 1
  • 16
  • 18
7

The :: refers to the global scope. For instance:

void f() { ... } // (1)

namespace ns
{
    void f() { ... } // (2)

    void g()
    {
        f(); // calls (2)
        ::f(); // calls (1)
    }
}
Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • -1 i already that :: is global. and it didnt answer what i aske, what is ':'. –  Jan 17 '11 at 05:37
  • @acidzombie24 Wait, you down vote me not because what I said is wrong (which it isn't), but because I told you something you already know? – Etienne de Martel Jan 17 '11 at 20:32
  • 1
    no, i voted down because you did not answer my question. > "What does the : ::name mean? " and "But what does the : do before it?" –  Jan 17 '11 at 22:52
3

Seems to be working with inheritance, extending from the global namespace.

class baseClass{ 
    public: 
    int someVal; 
};

class childClass : baseClass
{
    public:
    int AnotherVal;
}

Other answers provided later are much more robust.