Bloomberg LP Interview Question
Software Engineer / DevelopersA few differences:
1. If class that has an overloaded new operator, can not use base class's values to initialize its own values in its new operator. This is possible only in the constructor.
2. If new is overloaded for class B and D inherits from B, then "new D()" will call B's new operator. This behavior is different from how constructors behave.
3. If new [] is overloaded for class B then "new B[N]" gets only one chance to initialize N objects. You will need a loop. With constructors, however, one constructor will be called for each object. No loop needed.
why the hell not?
using overloaded new , u gotta first make space, then assign to object, then initialize it... constructor jus initializes the object for u...
Actually there is a global "operator new" that can be overridden on both a "per class" basis *or* globally overridden. It depends on whether the definition of the operator is within a class scope or the global scope.
- Wandering programmer February 28, 2010The default behavior of new is defined in the C++ standard, and does not vary. However, it the behavior if the new operator is overloaded, there is no guarantee that initialization (or that anything at all will happen). The use of the new operator will be governed by, and limited to, whatever functionality is in the overloaded function definition.