Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Smart-point...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 7 Topic 9545 of 9831
Post > Topic >>

Re: Smart-pointer pimpl and compiler-generated destructors.

by nickf3 <nickf3@[EMAIL PROTECTED] > Apr 25, 2008 at 04:13 PM

On Apr 25, 12:53 pm, rwf_20 <rfr...@[EMAIL PROTECTED]
> wrote:
> I'd like to use pimpl to reduce dependencies as follows:
>
> // myClass.h
>
> #include <memory>
>
> class myClass {
> public:
>    myClass();
>
> private:
>    struct myStruct;
>    std::auto_ptr<struct myStruct> m_pimpl;
>
> };
>
> // myClass.cpp
> #include "myClass.h"
>
> struct myClass::myStruct {
>     // ...details
>
> };
>
> myClass::myClass() : m_pimpl(std::auto_ptr<myStruct>(new myStruct()))
> { }
>
> This compiles just fine, until I try to use myClass elsewhere.  At
> this point, I get warnings like:
>
> "deletion of pointer to incomplete type 'myStruct'; no destructor
> called"
>
> I guess I understand why this happens, and it's fixed by declaring a
> destructor for myClass and defining it in the .cpp file.  It just irks
> me that I need to define a blank destructor (the same thing the
> compiler should generate) to make this work.  Googling this issue, I
> findhttp://www.gotw.ca/publications/using_auto_ptr_effectively.htm,
> in which Mr. Sutter demonstrates this same code and adds the comment:
>
> "In fact, if there's no other reason for explicitly writing a
> destructor, we don't need to bother with a custom destructor at all
> any more."
>
> I'm seeing that this is not the case.  Unless (likely) I am doing
> something wrong.  Thoughts?
>
> Thanks in advance,
>
> Ryan
>
> Is there a workaround for this issue?  I know it's only a few more
> lines of code

I think you got it pretty much right. There are only two choices -
either have the full declaration for the pointed-to class available,
or provide an explicit destructor implementation for the containing
class.

Since the std::auto_ptr is a template, the (inline) synthesized
destructor for
your containing class would include inline code for destructing the
auto_ptr member, which in turn calls the pointed-to class destructor,
which is not available with only forward declaration.

--
  Nikolai

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 7 Posts in Topic:
Smart-pointer pimpl and compiler-generated destructors.
rwf_20 <rfrenz@[EMAIL   2008-04-25 10:53:17 
Re: Smart-pointer pimpl and compiler-generated destructors.
Travis Vitek <travis.v  2008-04-25 15:48:58 
Re: Smart-pointer pimpl and compiler-generated destructors.
nickf3 <nickf3@[EMAIL   2008-04-25 16:13:20 
Re: Smart-pointer pimpl and compiler-generated destructors.
acehreli@[EMAIL PROTECTED  2008-04-25 16:13:19 
Re: Smart-pointer pimpl and compiler-generated destructors.
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-26 03:28:12 
Re: Smart-pointer pimpl and compiler-generated destructors.
Martin T <0xCDCDCDCD@[  2008-04-26 03:27:48 
Re: Smart-pointer pimpl and compiler-generated destructors.
Carl Barron <cbarron41  2008-04-26 03:34:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:54:19 CDT 2008.