Are empty initializers preferable for initializing integral elements by default?

I read a comment from from GMan , which

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

should be preferred

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

Note the absence NULLin the first example.

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

Also, if you prefer empty initializers, is this applicable to other integral elements?

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 point of view with a description of why you should be preferred over others.

+5
source share
4 answers

. , , , , int() int(0) .

, , , - .

+7

, , . , . , .

, :

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 ) {} 
};

NULL vs default initialization, : Visual Studio 2010, - ++ 0x NULL, - 0, ++ 0x nullptr . nullptr ++ '03. , , . ( ) .

+2

-, , , .:) , NULL; 0. .

, , , , , ; . .

, ; : "". , , , .

, , , , .

+2

I think these are different things, is it possible that you confuse NULLwith voidThe value is the int main()same as int main(void), but NOT int main(NULL)(In C ++ is NULLequivalent to 0

-1
source

All Articles