Your question should be:
How does the compiler distinguish between new B() and new B(10) when the syntax of B::operator new the same?
Well, new just allocates memory , and right after that, the compiler inserts a call to the constructor. Therefore, regardless of whether you call new B , new B() or new B(10) .
The compiler interprets something like:
B *b = static_cast<B*>(B::operator new(sizeof(B)))->B(); B *b1 = static_cast<B*>(B::operator new(sizeof(B)))->B(10);
In fact, the constructor returns nothing. But the above pseudo-code is just an analog representation of the internal material.
source share