On Apr 25, 9:53 am, rwf_20 <rfr...@[EMAIL PROTECTED]
> wrote:
> // myClass.h
>
> #include <memory>
>
> class myClass {
[...]
> std::auto_ptr<struct myStruct> m_pimpl;
>
> };
[...]
> myClass::myClass() : m_pimpl(std::auto_ptr<myStruct>(new myStruct()))
You can write this instead:
m_pimpl(new myStruct())
because that is an explicit construction.
> 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.
That's just what you have to do. Though I heard that the Boost
library's shared_ptr has machinery built in to overcome this. You
might want to look at how they did it.
> 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 don't have Exceptional C++ handy, but I bet Herb Sutter has
corrected that in his book. The reason that I am sure is that this
issue came up some time ago and was inspiration for me to write this
little piece:
http://www.accu-usa.org/2002-02-Main.html
There are mistakes in my text as well, but it proves to me that Herb
Sutter does define the destructor empty in the implementation file in
his book.
Ali
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|