Is there any way I can find how many objects are instantiated from stack and how many objects from Heap. I don’t wish to have the restrictions of scoping in objects from Stack.
If i use a static counter in constructor and destructor, it will be called in both the cases(object from stack and heap). One way is to exploit the idea are objects from stack are scoped and destructor is called when they go out of scope but i want a better solution so as to know the number of objects at any point of time.
Override operator new and operator delete for the class. Have another counter there that is incremented/decremented in these operators. This will keep track of objects created on the heap. The constructor/destructor can increment/decrement another counter that will count all objects. The difference between the two is the set of objects on the stack (as well as global objects).