Topic: auto_ptr and overriding
Author: Marc Girod <girod@trshp.trs.ntc.nokia.com>
Date: 1997/11/25 Raw View
>>>>> "VB" == Valentin Bonnard <bonnardv@pratique.fr> writes:
VB> Pete Becker wrote:
>> Ryszard Kabatek wrote:
>> > class Base {
>> > public:
>> > virtual auto_ptr<Base> clone() const = 0;
>> > };
>> >
>> > class Derived : public Base {
>> > public:
>> > virtual auto_ptr<Derived> clone() const; // Base::clone overriden?
>> > };
>> There's nothing special in auto_ptr to handle this.
VB> But there are members template in auto_ptr (a converting
VB> contructor and a convertion operator), and auto_ptr<Derived>
VB> to auto_ptr<Base> is allowed.
VB> Covariant return types only work on pointers, not other things
VB> implicitly convertible:
All this is right but... Ryszard's expectations are yet quite
reasonable (to me). OK, they don't work for auto_ptr.
How then to build smart pointers that would support that (can you
decently speak of smart _pointers_ if they don't? This has a smell of
Daniel Edelson's "they're smart but they're not pointers!")?
First, we'd have to reserve a common slot by having the Smart template
inherit from a common non-template base: SmartB, and have Base::clone
return it (well, maybe even a base of "Base", if one wants to expose
Base as an interface).
Next, we'd need that there would be only one slot, so that the
derivation from SmartB should be virtual. It should also be protected
but this is an other story.
The next point is that there could only be _one_ concrete
implementation per concrete hierarchy, so that Smart should in fact be
abstract, with a separate SmartImp containing the actual pointer, but
which couldn't be derived from.
I have hit no wall yet, have I?
Best Regards!
--
Marc Girod Nokia Telecommunications INS/IPS/MS/NOMA
Valimo 1/2 P.O. Box 315 Phone: +358-9-511 63331
00380 Helsinki 00045 NOKIA Group Fax: +358-9-511 63310
Finland marc.girod@ntc.nokia.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
Date: 1997/11/23 Raw View
Hi,
Could somebody tell me, please, what does the ANSI standard/draft say
about this example?
class Base {
public:
virtual auto_ptr<Base> clone() const = 0;
};
class Derived : public Base {
public:
virtual auto_ptr<Derived> clone() const; // Base::clone overriden?
};
My compiler, IBM VisualAge C++ 3.0 for OS/2, says: not overriden.
--
Ryszard Kabatek ,,,
(o o)
---------------o00--(_)--00o---------------
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/23 Raw View
Ryszard Kabatek <kabatek@chemie.uni-halle.de> writes:
>Could somebody tell me, please, what does the ANSI standard/draft say
>about this example?
>
>class Base {
> public:
> virtual auto_ptr<Base> clone() const = 0;
>};
>
>class Derived : public Base {
> public:
> virtual auto_ptr<Derived> clone() const; // Base::clone overriden?
>};
>
>My compiler, IBM VisualAge C++ 3.0 for OS/2, says: not overriden.
Your compiler is correct. Covariant return types only works for
built-in pointers, not for auto_ptrs.
See 10.3 [class.virtual] paragraph 5.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Pete Becker <petebecker@acm.org>
Date: 1997/11/24 Raw View
Ryszard Kabatek wrote:
>
> Hi,
> Could somebody tell me, please, what does the ANSI standard/draft say
> about this example?
>
> class Base {
> public:
> virtual auto_ptr<Base> clone() const = 0;
> };
>
> class Derived : public Base {
> public:
> virtual auto_ptr<Derived> clone() const; // Base::clone overriden?
> };
>
> My compiler, IBM VisualAge C++ 3.0 for OS/2, says: not overriden.
Your compiler is right. auto_ptr is just a template, and instantiating a
template for a derived type does not result in a template instance
that's convertible to the a template instance instantiated on the base
type. There's nothing special in auto_ptr to handle this.
template<class T> class X
{
};
I suppose it would have been possible to add a template member function
to do general type conversions, which would also handle derived to base,
but the consequences of that are too horrible to contemplate.
-- Pete
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/24 Raw View
Pete Becker wrote:
>
> Ryszard Kabatek wrote:
> > class Base {
> > public:
> > virtual auto_ptr<Base> clone() const = 0;
> > };
> >
> > class Derived : public Base {
> > public:
> > virtual auto_ptr<Derived> clone() const; // Base::clone overriden?
> > };
> Your compiler is right. auto_ptr is just a template, and instantiating a
> template for a derived type does not result in a template instance
> that's convertible to the a template instance instantiated on the base
> type.
Right in general, but...
> There's nothing special in auto_ptr to handle this.
> I suppose it would have been possible to add a template member function
> to do general type conversions, which would also handle derived to base,
> but the consequences of that are too horrible to contemplate.
But there are members template in auto_ptr (a converting
contructor and a convertion operator), and auto_ptr<Derived>
to auto_ptr<Base> is allowed.
Covariant return types only work on pointers, not other things
implicitly convertible:
int Base::func (); // virtual
char Der::func (); // no! and char is convertible to int
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/25 Raw View
Ryszard Kabatek <kabatek@chemie.uni-halle.de> writes:
> Could somebody tell me, please, what does the ANSI standard/draft say
> about this example?
>
> class Base {
> public:
> virtual auto_ptr<Base> clone() const = 0;
> };
>
> class Derived : public Base {
> public:
> virtual auto_ptr<Derived> clone() const; // Base::clone overriden?
> };
It's ill-formed, but you can rewrite it:
class Base {
public:
auto_ptr<Base> clone() const
{ return auto_ptr<Base> (clone_impl()); }
private:
virtual Base* clone_impl() const = 0;
};
class Derived : public Base {
public:
auto_ptr<Derived> clone() const; // non-virtual
{ return auto_ptr<Derived> (clone_impl()); }
private:
Derived* clone_impl() const;
};
In this case it's ok to hide (not overide) a function
(Derived::clone() hides Base::clone()).
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]