Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11824
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Any way to detect the absense of virtual destructor in base class? |
| Date | 2011-11-25 22:25 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <jap151$5an$1@dont-email.me> (permalink) |
| References | <jal28n$gpu$1@speranza.aioe.org> <jal34c$2k7$1@dont-email.me> <4ecfdede$0$4348$7b1e8fa0@news.nbl.fi> |
On 25.11.2011 19:30, Juha Nieminen wrote: > Alf P. Steinbach<alf.p.steinbach+usenet@gmail.com> wrote: >> Use `std::unique_ptr` or `std::shared_ptr`, or e.g. `boost::shared_ptr`. >> >> These smart pointers remember the proper derived class destruction to >> use, freeing you having to have a virtual destructor. > > How exactly do they achieve that? If I do, for example, this: > > std::unique_ptr<Base*> ptr = new Derived; > > then how exactly is 'ptr' able to deduce the derived class destructor in > order to directly call it when it disposes of the object? > > (And where would it store it anyways? I thought the whole idea with > std::unique_ptr is that its size is that of one pointer, hence making > it as efficient as a raw pointer.) > > I wonder if you are being confused by shared_ptr (and possible other > smart pointers) being able to destroy an object even if they only see > a class declaration (rather than a definition), as long as the class was > fully declared at the point of construction of the pointer. (IOW the class > doesn't need to be fully declared at the place of destruction.) That's not > the same thing as a base-class type smart pointer being able to deduce > the derived-class type destructor. I'm looking at N3290, which I believe is identical to the final standard. And there std::unique_ptr stores a deleter, which can be function object or a function pointer or whatever, specified in constructor call. Essentially it's the same idea as for shared_ptr's deleter except that that unique_ptr doesn't use templating to infer the real type of the object pointer it gets, so one has to write like unique_ptr<Base> p = unique_ptr<Derived>( new Derived ); The only trouble is, I can't get the deleter functionality to work with either Visual C++ 10.0 or g++ 4.4.1 in Windows, nor with g++ 4.6.1 in Ubuntu. That might indicate that my understanding is wrong (in which case the deleter argument then seems worthless), or that unique_ptr is not fully implemented by these compilers, or that N3290 is not identical to C++11? Cheers, & thanks for focusing me on that, - Alf
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Any way to detect the absense of virtual destructor in base class? Qi <no@no.no> - 2011-11-24 17:20 +0800
Re: Any way to detect the absense of virtual destructor in base class? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-24 10:35 +0100
Re: Any way to detect the absense of virtual destructor in base class? Qi <no@no.com> - 2011-11-24 21:05 +0800
Re: Any way to detect the absense of virtual destructor in base class? Juha Nieminen <nospam@thanks.invalid> - 2011-11-25 18:30 +0000
Re: Any way to detect the absense of virtual destructor in base class? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-25 22:25 +0100
Re: Any way to detect the absense of virtual destructor in base class? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-25 22:40 +0100
Re: Any way to detect the absense of virtual destructor in base class? Juha Nieminen <nospam@thanks.invalid> - 2011-11-25 22:30 +0000
Re: Any way to detect the absense of virtual destructor in base class? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-26 01:36 +0100
Re: Any way to detect the absense of virtual destructor in base class? Juha Nieminen <nospam@thanks.invalid> - 2011-11-26 12:16 +0000
Re: Any way to detect the absense of virtual destructor in base class? Vladimir Jovic <vladaspams@gmail.com> - 2011-11-24 10:45 +0100
Re: Any way to detect the absense of virtual destructor in base class? Qi <no@no.com> - 2011-11-24 21:07 +0800
Re: Any way to detect the absense of virtual destructor in base class? SG <s.gesemann@gmail.com> - 2011-11-24 04:50 -0800
Re: Any way to detect the absense of virtual destructor in base class? Qi <no@no.com> - 2011-11-24 21:04 +0800
Re: Any way to detect the absense of virtual destructor in base class? Marc <marc.glisse@gmail.com> - 2011-11-24 16:12 +0000
Re: Any way to detect the absense of virtual destructor in base class? Lee Steven <steven.mtl@gmail.com> - 2011-11-25 06:55 -0800
Re: Any way to detect the absense of virtual destructor in base class? jgk@panix.com (Joe keane) - 2011-11-25 22:21 +0000
Re: Any way to detect the absense of virtual destructor in base class? Ian Collins <ian-news@hotmail.com> - 2011-11-26 11:32 +1300
csiph-web