c++ - The destructor for the class member `B`, why is it invoked in the snippet below? -


from §5.3.5[expr.delete]/1, can understand destructor object *a not invoked in snippet below. didn't understand why destructor class member b invoked in case, can seen in live example.

#include <iostream> class { public:    class b{ public: ~b(){ std::cout << "b dtor" << '\n'; } };    a() { p = new b(); }    operator b*() { return p; } private:    b* p; };  int main() {    a* = new a();    delete *a;    std::cout << "end" << '\n'; } 

would appreciate quote standard explaining this.

your delete *a applies operator delete non-pointer expression *a of type a. way can legal when type a implicitly convertible pointer type.

5.3.5 delete [expr.delete]

1 ... operand shall have pointer object type, or class type having single non-explicit conversion function (12.3.2) pointer object type.

2 if operand has class type, operand converted pointer type calling above-mentioned conversion function, , converted operand used in place of original operand remainder of section.

in case class a implicitly convertible b *, happens when delete *a.

in other words, delete *a interpreted as

delete (*a).operator b*(); 

it b delete in code, not a. why destructor of b called.

if wanted destroy a object, you'd have do

delete a; 

(note, no *). not call b's destructor.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -