c++ - Undefined behavior during destrction? -


let's consider following code:

#include <iostream>  struct a{ virtual void foo(){ } };  struct b : { virtual void foo(){ } };  *a = new b;   int main() {     delete a; //ub? } 

i deliberately didn't define virtual destructor. compiler printed message causing ub, true?

formally have ub if delete through pointer t, not derived type, , t doesn't have virtual destructor.

in practice can away if don't have data mambers, it's still ungood , unnecessary practice.

note: when use shared_ptr creates deleter function @ point of initialization, , deleter function can remember original type, which, if type derived type, ensures well-defined deletion. e.g. in case shared_ptr<a> p( new b ); ok.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -