auto_ptr conversion confusion

John Love-Jensen eljay@adobe.com
Fri Oct 11 13:02:00 GMT 2002


Hi Kristian,

Even though a "B" is an "A".  (Structs inherit their superclasses as public
by default.)  This "auto_ptr<B>" is not one an "auto_ptr<A>".

Fix your code like this:

 1 #include <memory>
 2 using namespace std;
 3
 4 struct A {};
 5 struct B: A {};
 6
 7 auto_ptr<A> f()
 8 {
 9   auto_ptr<A> b(new B);
10   return b;
11 }
12
13 int main()
14 {
15   auto_ptr<A> a = f();
16   return 0;
17 }

And it should work, at least with GCC 3.2.

--Eljay




More information about the Gcc-help mailing list