c++ destroy an object on stack frame -


i try understand happen when object destroy on stack. here sample code:

#include <stdio.h> struct b {   ~b() {puts("bbbb");} };  int main() {     b b;     b.~b(); } 

output

bbbb bbbb 

based on output, can tell object destroy twice.one ~b(), 1 after "}". how , why can object destroy twice?

update: after review replies, think destructor doesnt destroy object. there way destroy object before reach out of scope "}". thanks

you not supposed invoke destructor hand. what's happening invoking destructor , when object gets popped off stack destructor called again automatically compiler.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -