views:

95

answers:

4

I just read a comment by GMan that

class A
{
public:
   A() :
      m_ptr() // m_ptr is implicitly initialized to NULL
   { }
};

should be preferred over

class A
{
public:
   A() :
      m_ptr(NULL) // m_ptr is explicitly initialized to NULL
   { }
};

Notice the lack of NULL in the first example.

Is GMan right? This might kinda subjective, so "Do you prefer empty initializers for default initialization?" might be more appropriate.

Also if you prefer empty initializers, do does this apply to other integral members?

class B
{
public:
   B() :
      m_count(),
      m_elapsed_secs()
   {}
private:
   std::size_t m_count;
   float m_elapsed_secs;  //the elapsed time since instantiation
};

Of course, please defend your view point with a description of why one should be preferred over the other.

A: 

I think they are different things, is it possible you are confusing NULL with void meaning int main() is the same as int main(void) but NOT int main(NULL) (In C++ NULL is equivalent to 0

Jordan
There are no function definitions in my question. I'm asking about initializing member in a class's initializer list.
caspin
+5  A: 

I prefer the explicitness. As some of the wrong answers to this question have demonstrated, it's not obvious to everyone that, say, int() and int(0) are equivalent.

I suppose not supplying an explicit value has the advantage that you won't need to revisit the initialization list if you ever change the type.

jamesdlin
You could argue that not needing to revisit the initialization list when the type changes is a negative (when default init is no longer what you want for the new type).
Mark B
+2  A: 

Default initialization is necessary when you write a template class to default initialize members of dependent types. For other cases there're no real difference if you want to default initialize the member. But there are some cases when you want to get non default behavior.

One sample when default initialization is not suitable:

struct X {
  HANDLE some_file_handle;
  // note that INVALID_HANDLE_VALUE is not equal to 0 in Windows
  X() : some_file_handle( INVALID_HANDLE_VALUE ) {} 
};

As for using NULL vs default initialization I have one more example: in Visual Studio 2010 which is declared to be somehow conformant with C++0x NULL is still defined as 0, but in C++0x you should use nullptr to initialize pointers. And nullptr is not defined in C++'03. But you probably want to make your code portable. In that case (to initialize pointers) I will prefer a default initialization over value initialization.

Kirill V. Lyadvinsky
+3  A: 

Firstly, I said it's arguably better, not that it is. :) Also, it was more about getting rid of NULL; I just happen to use nothing instead of 0. But an interesting question anyway.

It's probably just a matter of style, but it's important to note, as Johannes did, that it's not just syntactical style; they do different things. It's just easy to make those different things the same.

I prefer value-initialization, because I'm not taking any part of how the value is being initialized; I'm simply saying "be initialized." Contrarily, once you introduce a value you are influencing how the value is initialized.

I think you'd be hard-pressed to find a situation where value-initialization is clearly better, just pick which one suits you more.

GMan
Do you prefer value initialization in the case of `class B`? To me, it felt a little weird not initializing a count variable to 0.
caspin
I think that GMan shouldn't answer the question "Is GMan right". It's some kind of recursion. :)
Kirill V. Lyadvinsky
@Caspin: Yup. I'm just comfortable knowing it will be initialized to 0 anyway, so I feel specifying the constant anyway is just a waste. :) Then again, being explicit can be easier to read.
GMan
I'd initialize to 0 explicitly. Yes, I know that's what will happen with value-initialization as well, but I think I typically *am* "taking part of how the value is being initialized". I want it to be initialized to 0 specifically, because my code is (probably) going to rely on that being the case. The reason I'm doing the initialization at all is because I want the value to be 0. But very subjective, obviously, and reading code that relies on value initialization doesn't bother me the least.
jalf