Class B;
B *b = new B(); // default constructor
B *b1 = new B(10); // constructor which takes an argument B(int x)
However, if we want to write a custom version of new, the syntax is
Class B
{
/*...*/
static void* operator new(size_t size);
}
How is the statement new B() converted to a function call for
operator new(sizeof(B))?
And how does it keep track of which constructor to call i.e. how does it distinguish between new B() and new B(int x)?
Is new implemented as a macro in C++?
Your question should be:
Well,
newjust allocates the memory and immediately after that the compiler inserts the call to the constructor. So it’s irrespective if you callnew B,new B()ornew B(10).Compiler interprets something like:
In actual a constructor doesn’t return anything. But above pseudo code is just an analogical representation of internal stuff.