开发者

Are empty initializers preferred for default initializing integral members?

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 me开发者_如何学编程mbers?

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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜