2

I want to ask what does the (0) mean after the pointer i.e. Node* ptr1(0).

struct Node
{
   string info;
   Node * next
};

int main()
{ 
   Node* ptr1 (0), *ptr2 (0),
   ptr1 = new Node;
   ptr2 = new Node;
}
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
Arvin Wong
  • 133
  • 1
  • 1
  • 9
  • 4
    It should be noted that instead of initialising pointers with `0` or `NULL` one should use `nullptr` instead as of c++ 11. – sjrowlinson May 20 '16 at 15:05

5 Answers5

3

It means initializing the pointer with 0 or null.

Refer to this question for understanding the explanation behind it (they're actually the same; 0 and null in this context)

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
1

For any integral type T, the following two declarations are effectively equivalent:

T obj(0);
T obj = 0;

And since 0 is a null pointer constant, all you're doing here is initialising your two pointers to be null.

There are lots of ways to initialise objects, but consider how you declare objects of class type:

MyClass obj(someArguments...);

It's the same thing.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

This is direct initialization.

if T is a non-class type, standard conversions are used, if necessary, to convert the value of other to the cv-unqualified version of T.

For pointer type, initialize it with 0 makes it a null pointer. See Pointer conversions.

A null pointer constant (see NULL), can be converted to any pointer type, and the result is the null pointer value of that type.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

This simply initializes the pointer. And it is initializing it to be null.

Destructor
  • 523
  • 1
  • 3
  • 13
0

This is a constructor call. Since there is no constructor defined the compiler supplies one. After C++11 the preferred form is 'Node * ptr1 {0};' using braces.

Gregg
  • 2,444
  • 1
  • 12
  • 21