What is this weird constructory syntax C::C()?

Raymond Chen

Raymond

The Microsoft Visual C++ compiler supports this weird thing:

// assume a class C has been defined
C* p = (C*)malloc(sizeof(C));
p->C::C(); // huh?

This weird syntax is how people in olden times explicitly invoked a constructor on an uninitialized block of memory.

Then placement new arrived on the scene and made the above syntax obsolete.

// new hotness
C* p = (C*)malloc(sizeof(C));
new(p) C();

But the Microsoft Visual C++ compiler still supports the old syntax for backward compatibility purposes.

Note that the corresponding explicit destructor syntax

p->C::~C(); // can be shortened to p->~C() if p is of type C*

is still standard as of this writing.

Raymond Chen
Raymond Chen

Follow Raymond