8

I have seen the related answers here and here on this, but I wanted confirmation, because none of them make this explicit.

Suppose I have a class Foo and a member bar of type int*.

Are the following two initializations entirely equivalent?

Foo::Foo() : bar(NULL) // null pointer constant by macro
{
}

Foo::Foo() : bar() // value initialization
{
}
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329

1 Answers1

13

Value-initialization of a pointer initializes it to a null pointer value; therefore both initializer lists are equivalent.


Pointers don't have class or array type, so value initialization for them is zero initialization. (8.5p8)

Then, (8.5p6)

To zero-initialize an object or reference of type T means:

  • if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T

This integer literal 0 is a null pointer constant (4.10p1), which when converted to a pointer type creates a null pointer value.


Note that zero-initialization of variables with static and thread duration (3.6.2) also initializes pointers to null pointer values.


Above paragraph references are from C++1y draft n3936, but they were the same in earlier drafts I checked also.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720