Topic: ANSI/ISO Resolution r.10.2


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sun, 30 Oct 1994 07:48:31 GMT
Raw View
In article <383sa0$f9l@carbon.denver.colorado.edu> tgibson@evans.cudenver.edu (Todd Arthur Gibson) writes:
>
>This resolution relaxes the requirement for overriding virtual functions.
>(i.e. different return types in base and derived classes)
>
>This doesn't mean that I now have dynamic typing does it?
>See example below:

I don't believe so, no.  The compiler still has to resolve overloading
by using the _static_ types of all argument expressions.


>class B {
>  //...
>  virtual B* typeMe() { return (B*)this };
>};
>
>class D : public B {
>  //...
>  virtual D* typeMe() { return (D*)this };
>};
>
>someFunc(B* base)
>{
>  cout << "in Base version\n";
>}
>
>someFunc(D* derived)
>{
>  cout << "in Derived version\n";
>}
>
>main()
>{
>   B* bp=new D;
>
>   someFunc( bp->typeMe() );
>}
>
>--
>Todd A. Gibson
>Reply To: tgibson@lookout.ecte.uswc.uswest.com
>"Life is like a box of chocolates:
>You never know when your going to get one of those horrible coconut ones."


--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@rahul.net ----------------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: tgibson@evans.cudenver.edu (Todd Arthur Gibson)
Date: 19 Oct 1994 19:33:20 GMT
Raw View
This resolution relaxes the requirement for overriding virtual functions.
(i.e. different return types in base and derived classes)

This doesn't mean that I now have dynamic typing does it?
See example below:


class B {
  //...
  virtual B* typeMe() { return (B*)this };
};

class D : public B {
  //...
  virtual D* typeMe() { return (D*)this };
};

someFunc(B* base)
{
  cout << "in Base version\n";
}

someFunc(D* derived)
{
  cout << "in Derived version\n";
}

main()
{
   B* bp=new D;

   someFunc( bp->typeMe() );
}

--
Todd A. Gibson
Reply To: tgibson@lookout.ecte.uswc.uswest.com
"Life is like a box of chocolates:
You never know when your going to get one of those horrible coconut ones."




Author: davidc@bruce.cs.monash.edu.au (David Chatterton)
Date: 20 Oct 1994 23:19:12 GMT
Raw View
Todd Arthur Gibson (tgibson@evans.cudenver.edu) wrote:

: This resolution relaxes the requirement for overriding virtual functions.
: (i.e. different return types in base and derived classes)

: This doesn't mean that I now have dynamic typing does it?
: See example below:


: class B {
:   //...
:   virtual B* typeMe() { return (B*)this };
: };

: class D : public B {
:   //...
:   virtual D* typeMe() { return (D*)this };
: };

: someFunc(B* base)
: {
:   cout << "in Base version\n";
: }

: someFunc(D* derived)
: {
:   cout << "in Derived version\n";
: }

: main()
: {
:    B* bp=new D;

:    someFunc( bp->typeMe() );
: }

Still calls someFunc(B*) as bp is a B* and a compiler cannot determine
which virtual function will be called. So it will make the decision
based on the return type of the base virtual function.

eg.

class F : public D
{
 virtual F* typeMe()
  { return (F*)this; }
};

someFunc(F*);

main()
{
 D* dp = new F;

 someFunc ( dp->typeMe() );    // calls someFunc(D*)
}

If this is not what you want, then you need something like multi-methods.

The idea of allowing the return type to be a derived type is so that
people do not have to do a dangerous downcast when they don't need to.
It is a great extension as it does not break the existing behaviour
of virtual functions.

David


David Chatterton     | "A new character has come on the scene (I am sure I did
Comp Sci Department, | not invent him, I did not even want him, though I like
Monash Uni, Clayton, | him, but there he came, walking out of the woods of
Australia, 3168.     | Ithilien): Faramir, the brother of Boromir."
Phone: 03 905 5375   | - in a letter from JRR Tolkien to his son, 4 May 1944.
email: davidc@bruce.cs.monash.edu.au