user6605865
user6605865

Reputation:

c++ how to read this creating new constructor code?

LinkedListNode* toSort = new LinkedListNode((MTNode*)0, (LinkedListNode*)0,
        (LinkedListNode*)0);

can someone give me detail explanation on how to read this code?? especially, I don't understand those zeros in

(MTNode*)0, (LinkedListNode*)0,(LinkedListNode*)0

Upvotes: 2

Views: 48

Answers (2)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145224

Literal 0 designates a nullpointer where a pointer is expected. The casts to the relevant pointer types are ¹redundant. In modern C++ just write nullptr, and preferably use curly braces for constructor invocations, i.e.

auto* toSort = new LinkedListNode{ nullptr, nullptr, nullptr };

Notes:
¹ Unless there are two or more overloads that accept literal 0 arguments; in that case the casts can serve to disambiguate the call, choose the right constructor.

Upvotes: 3

In a bit more modern c++, it would look like:

LinkedListNode* toSort = new LinkedListNode(nullptr, nullptr, nullptr);

Does it make more sense now? It just passes null values for each expected pointer.

Upvotes: 4

Related Questions