-1

I am going through the "Getting started with the Windows API" portion of MSDN, and ran into an aspect of C++ I'm having trouble understanding.

I've been looking in "The C++ Programming Language" by Stroustrup, and can't find this particular thing.

Please check the link to the relevant page on MSDN.

The line that I don't understand is:

BaseWindow() : m_hwnd(NULL) { }

This line is in the latter code snippet describing an object oriented approach to the subject. m_hwnd is of the type HWND.

I really have no idea what is happening here. I'm pretty sure it's a constructor that doesn't do much of anything, but this specific syntax is foreign to me. I must admit my C++ knowledge is mostly just "C with some C++ stuff added". I'm currently diving deep into Stroustrup to remedy that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dan Moos
  • 23
  • 4
  • The book you're reading *should* explain it. Perhaps if you knew the right term for it, it would be easier to find? The correct term is *constructor initializer list*. – Some programmer dude Jun 19 '18 at 03:09
  • Ok, I found the relevant portion of the book. That seems to have given me clarity. You're right, my lack of the term I needed to search was my main difficulty. Tell me if I have this right. A class derived from BaseWindow gets its m_hwnd member set to NULL upon object instantiation. Is that right? The colon is still fuzzy to me here. – Dan Moos Jun 19 '18 at 03:16
  • Dan Moos - that is right, I think this is part of derived class. – SJHowe Jun 19 '18 at 03:22
  • Dan Moos - that is right, I think this is part of derived class. BaseWindow() - this calls the BaseWindow default constructor (BaseWindow might have other constructors). The colon says the base class constructors calls are over. m_hwnd(NULL) - this is doing derived member initialisation { } - main body of derived constructor, empty, all base classes and members initialised by this point. – SJHowe Jun 19 '18 at 03:28

2 Answers2

2

The colon is just a syntax divider between the constructor declaration and the constructor initializer list.

It's a way to directly construct (or initialize) member variables.

It's similar (but not equal) to

BaseWindow()
{
    m_hwnd = NULL;
}

Another way to look at it: Lets say you define a normal local variable, and want to initialize it to a specific value. There are a couple of ways to doing it.

  1. Definition (with default initialization) and assignment:

    HWND hwnd;
    hwnd = 0;
    
  2. Definition and copy initialization:

    HWND hwnd = 0;
    
  3. Definition and direct initialization:

    HWND hwnd(0);
    

With a constructor initializer list, you use direct initialization in the constructor, and it happens before the constructor function body is executed.

Note that templates doesn't play any part of this. And neither does inheritance, even though it's possible to "call" a parent class constructor using a constructor initializer list.

Lastly an important note: Constructor initializer lists should not be confused with std::initializer_list.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

BaseWindow() : m_hwnd(NULL) { }

That declares a default constructor for BaseWindow, which initializes the m_hwnd with a NULL value.

It does not, however, guarantee that m_hwnd will always be initialized to NULL. For instance, the default copy constructor will not do that. An explicit copy constructor is not likely to do it, either.

Sid S
  • 6,037
  • 2
  • 18
  • 24