What exactly is happening in the code below.
#include<iostream.h>
class Demo
{
public :
Demo()
{
cout<<"\nIn Demo const";
}
~Demo()
{
cout<<"\nin demo dest";
}
};
void main() {
Demo();
}
Demo()
simply calls the constructor and destructor. Is a object being created in this process? And thus is the memory being allocated?
You're not explicitly calling the constructor, instead this code creates a temporary unnamed object with type Demo
, which is destroyed immediately after ;
.
Yes, memory is allocated (automatically, on the stack) for this temp object and it's freed (again automatically) after ;
. Meanwhile, the constructor and destructor are called, as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With