TATA Consultancy Services Interview Question
Country: India
Class B object can't be created because its destructor is private. When you compile code, compiler adds constructor/destructor implicitly for local objects(objects on stack). However, since destructor is private, it won't be found and hence compilation will fail. You could refer compiled code of the program for more clarity.
In case of objects created in heap via new, compiler won't call constructor/destructor implicitly and it is the responsibility of the programmer to call them. Hence, it will compile properly.
Class B has a private Destructor, therefore not allowed on stack.
- Varun March 18, 2016