Re: auto_ptr compile error
George wrote:
Hi David,
1.
Moving the definition of ~Owner() to where Thing has been fully defined will get
rid of the problem.
I do not quite understand what do you mean moving the definition ... to
where...
Do you mean move definition of ~Owner() before full declaration of Thing or
after full declaration of Thing?
2.
I think the root cause is, we should declare a complete type before using it
as template type parameter of auto_ptr. I do not think the error has
something to do with destructor of ~Owner() as you mentioned, but the
destructor of Thing.
Any comments?
I have fixed it by changing owner.h by this way.
[Code]
#pragma once
#include <memory>
#include <iostream>
using namespace std;
class Thing
{
public:
Thing() { }
~Thing() { std::cout << "~Thing\n"; }
};
class Owner
{
public:
Owner();
private:
std::auto_ptr<Thing> thing;
};
[/Code]
George:
Your questions:
1. Obviously I meant *after* Thing has been defined.
2. Obviously also, your change will work. Otherwise auto_ptr could never be used
as a member.
The whole question whether you can have an auto_ptr member using a class (say,
Thing) that has only been forward declared. According to the standard it appears
not, but in practice it seems to work if the destructor of the containing class
is defined after Thing has been fully defined.
There seems general agreement that "deletion of incomplete type" should be an
error, rather than a warning, because it can lead to memory leaks.
It also seems to me that the general prohibition on using incomplete types as
template parameters is overly strict.
--
David Wilkinson
Visual C++ MVP