Topic: Using auto_ptr with abstract class


Author: "Guillaume Roques" <groques@tf1.fr>
Date: 1998/08/21
Raw View
Hello,

I want to use the STL auto_ptr class within a framework but the operator "
*() " witch return a reference on the class pointed by auto_ptr prevent the
use of abstract class as the pointed class.

For exemple I can't use this statement :

class TAbstract
{
    ...
   void Method1() = 0;
    ...
}

class TConcrete : public TAbstract
{
    ...
    void Method1();
    ...
}

void main()
{
    // This is OK
    TAbstract* generic1 = new TConcrete;
    delete generic1;

    // Can't compile this code because of the * operator in auto_ptr...
    auto_ptr<TAbstract> generic2(new TConcrete); // error
}

How make a new (?) class with the same semantic as auto_ptr but witch accept
abstract class as template parameter...

Thanks in advance.





[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/08/21
Raw View
On 21 Aug 1998 16:53:04 GMT, Guillaume Roques <groques@tf1.fr> wrote:

>I want to use the STL auto_ptr class within a framework but the operator "
>*() " witch return a reference on the class pointed by auto_ptr prevent the
>use of abstract class as the pointed class.

There's no problem.  A reference is pretty much the same as a pointer.
A new class is not created.  You still have polymorphism -- i.e. there
is no slicing and virtual functions work.

The problem is returning a value.  But auto_ptr does not do this.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]