The meaning of the collon within a class definition

Hello everybody.
I am trying to understand the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CSimpleDict
{
public:
    CSimpleDict( HINSTANCE hInstance ) :
            m_hInstance( hInstance ),
            m_bGotReco( FALSE ),
            m_bInSound( FALSE )
    {}
    //(...)
    HINSTANCE                   m_hInstance;
    HWND                        m_hDlg;
    BOOL                        m_bInSound;
    BOOL                        m_bGotReco;
    //(...)
};


My question is:

Why to use the collon (:) after the declaration of the constructor?
1
2
3
4
5
6
public:
    CSimpleDict( HINSTANCE hInstance ) : // <--- this collon!
            m_hInstance( hInstance ),
            m_bGotReco( FALSE ),
            m_bInSound( FALSE )
    {}


Are the three variables following the declaration being initialized?
If so, why not to do it within the pair {} and how can they be initialized before their declaration?

Thanks in advance.
Last edited on
I believe it's an initialization list. Look up the initializer list in the articles forum section; it should clarify.
closed account (jwC5fSEw)
That's the constructor initializer list. Its sole purpose is to initialize members.

Also, it can initialize them before their declaration because the compiler waits for the closing brace of the class before looking for members in the class. (I'm sure there's a more proper way of phrasing that, but I can't really think of one)
Just as an aside, it's important to note the difference between the initializer list and the body of the constructor.
The variables are declared the minute you create the class object, constructor or no, afaik. But if you do the "initializing" in the body of the constructor, it's actually assignment - obliteration of the previous value followed by the instantiation of a new value. The initializer list, on the other hand, initializes the variables directly. It's like the difference between
1
2
3
4
int var;
var = 10; // assignment

int var = 10; // initialization 
Thank you for the prompt response.
But let me ask again...

Shadow Addict said >> "That's the constructor initializer list. Its sole purpose is to initialize members."

Ok. But shouldn't they be initialized within the braces {}? like this:
1
2
3
4
5
CSimpleDict( HINSTANCE hInstance ) {
            m_hInstance = hInstance;
            m_bGotReco = FALSE;
            m_bInSound = FALSE;
}

Or we can do it also with collon (just like in the example)?
I mean, do the lines I just wrote produce the same efect as in the first post?
Note what I just said. There is actually a difference, in terms of effects, between initializing in the body (curly braces) and the initializer list (following a colon).
Thank you all for the help.
tummychow,
I hadn't seen your answer when I replied. I could understand after your "int var" example =)
I also just read an article about the difference between initialization and assigment and it became clear.
Topic archived. No new replies allowed.