6
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++?

jogojapan
  • 63,098
  • 9
  • 87
  • 125
vamsi
  • 1,507
  • 3
  • 19
  • 25
  • The work is all done by the compiler knowing how to replace operators and such. – Jesus Ramos Mar 07 '12 at 04:17
  • There is a difference between `new operator` and `operator new`. Check this question: http://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new – Naveen Mar 07 '12 at 04:18
  • [There is a difference between `new B()` and `new B`](http://stackoverflow.com/questions/1581763/difference-between-a-pa-new-a-and-a-pa-new-a). There's also a difference between `Class B {}` and `class B {};` - the former has syntax errors, the latter is how you declare classes. – In silico Mar 07 '12 at 04:18
  • Just take any c++ book and go to operator overloading – Rohit Vipin Mathews Mar 07 '12 at 04:19

2 Answers2

8

Your question should be:

How compiler distinguish between new B() and new B(10), when the B::operator new syntax is same ?

Well, new just allocates the memory and immediately after that the compiler inserts the call to the constructor. So it's irrespective if you call new B, new B() or new B(10).

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 actual a constructor doesn't return anything. But above pseudo code is just an analogical representation of internal stuff.

iammilind
  • 62,239
  • 27
  • 150
  • 297
  • There is C++ syntax for calling a constructor explicitly: `new (B::operator new(sizeof(B))) B()`. – Jon Purdy Mar 07 '12 at 04:34
  • 3
    There is a difference between `new T` and `new T()` for some types. And `operator new` is the one that just allocates memory — ordinary `new` does both allocation and construction, so it's wrong to say it only allocates. There's also placement `new` that only does construction, as mentioned by @Jon (as you can't call the constructor directly). – Cat Plus Plus Mar 07 '12 at 04:53
  • 1
    @iammilind: I was just offering an example showing how it’s possible to implement yours. – Jon Purdy Mar 07 '12 at 17:21
0

The question of which constructor to call is a matter of overload resolution based on the argument list, similar to any overloaded function call. At the site where new B(...) occurs, all the information is available. The compiler can resolve the reference to class B (name lookup), and see the portfolio of constructors available, and also see that B has a custom memory allocator mechanism. The compiler can emit the code to use that memory allocator to get the space (passing in the size of B), and then invoke appropriate constructor code to initialize the object in that space.

Kaz
  • 48,579
  • 8
  • 85
  • 132