Topic: abstract functions of abstract classes returning abstract objects
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Fri, 29 Jul 1994 07:26:48 GMT Raw View
In article <1994Jul27.103403.5291@leeds.ac.uk> garyt@resumix.portal.com writes:
>I have an abstract base class, an ABC, with abstract functions. what I want
>to know is why can't abstract functions return abstract classes of
>the same type as the abstract class or one of it's base classes from
>a virtual function.
>
>otherwise how do you use an abc as pure interface class when some of you
>normal ops such as operator + are illegal
You cant. You couldnt implement + anyhow.
It would be possible to implement a unary operator polymorphically,
the problem is storage management. If C++ had garbage collection,
class X {
X& operator!()const=0; // return new object
};
would make sense in conjunction with covariant returns. C++ doesnt
have garbage collection. Binary operators cant be polymorphic.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: garyt@resumix.portal.com (Gary Thompson)
Date: Wed, 27 Jul 1994 10:34:03 GMT Raw View
I have an abstract base class, an ABC, with abstract functions. what I want
to know is why can't abstract functions return abstract classes of the same type as the abstract class or one of it's base classes from a virtual function. ie has this been made legal
class A
{
virtual A func() = 0; /// declare interface
};
so that in a superclass you can write
class B
{
B func(); //redeclare interface
};
B B::func() // make it real
{}
otherwise how do you use an abc as pure interface class when some of you normal ops such as operator + are illegal
gary
Author: olaf@cwi.nl (Olaf Weber)
Date: Wed, 27 Jul 1994 12:08:42 GMT Raw View
In article <1994Jul27.103403.5291@leeds.ac.uk>, garyt@resumix.portal.com (Gary Thompson) writes:
> I have an abstract base class, an ABC, with abstract functions. what
> I want to know is why can't abstract functions return abstract
> classes of the same type as the abstract class or one of it's base
> classes from a virtual function.
The following has been made legal:
struct A {
virtual A *func() = 0;
};
struct B: public A {
B *func();
};
And it is also legal for references. This can only be done for the
return type though. (ARM, chapter 19, section 10.2)
-- Olaf Weber
Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 27 Jul 1994 13:39:10 -0500 Raw View
garyt@resumix.portal.com (Gary Thompson) writes:
>I have an abstract base class, an ABC, with abstract functions. what I want
>to know is why can't abstract functions return abstract classes of the same type as the abstract class or one of it's base classes from a virtual function. ie has this been made legal
>class A
>{
> virtual A func() = 0; /// declare interface
>};
>so that in a superclass you can write
>class B
>{
> B func(); //redeclare interface
>};
>B B::func() // make it real
>{}
>otherwise how do you use an abc as pure interface class when some of you normal ops such as operator + are illegal
You must change your virtual function to return a A* (pointer to A)
or a A& (reference to A) if you wish to have a child class change
the return type.