Destructor direct call in C++ -


hence code below.

class {     int x; public:     a() {x = 3;} };   int main() {     void* mem = operator new(sizeof(a));     a* obj = static_cast<a*>(new(mem)(a));     std::cout << obj->x << std::endl;     obj->a::~a();     std::cout << obj->x << std::endl; } 

my first question is: why can directly call destructor of a; second question is: why output is:

3 3 

the object obj not deleted after destructor call? second 3 bothers me.

why can call destructor?

because public member function, , can call public member functions.

why object not deleted?

in specific case, still exists because a has trivial destructor.

if had non-trivial one, deleted in sense not allowed use anymore. if anyways, have undefined behavior.

for more detailed discussion on read this.


Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -