Topic: Q on virtual functions example


Author: bkline%occs.nlm.nih.gov (Bob Kline Phoenix Contract)
Date: 1995/05/25
Raw View
They say it's a bad sign when you start posting followups to your
own articles :->}

On May 22, Bob Kline (bkline@occs.nlm.nih.gov) blathered away, writing:
: In section 10.3, paragraph 5, in the draft submitted for public review,
: there is an example given which has me baffled.  Here it is:

:   class B {};
:   class D : private B { friend class Derived; };
:   struct Base {
:     virtual void vf1();
:     virtual void vf2();
:     virtual void vf3();
:     virtual B*   vf4();
:     void f();
:   };

:   struct No_good : public Base {
:     D* vf4();       // error: B (base class of D) inaccessible <------*!!
:   };

:   struct Derived : public Base {
:     void vf1();     // virtual and overrides Base::vf1()
:     void vf2(int);  // not virtual, hides Base::vf2()
:     char vf3();     // error: invalid difference in return type only
:     D* vf4();       // okay: returns pointer to derived class  <------*!!
:     void f();
:   };

:   ....

: Could someone please help me understand why the function vf4() is legal
: in Derived and not in No_good?  They look like the same situation to me.

Ouch!  I see the friend statement now.  The only (very feeble) explanation
I can think of for the blind spot is the discrepancy between 'class
Derived' and 'struct Derived' -- I hope they won't turn the author
of these examples loose to do tutorials on good programming style :->}.
Mea culpa!

: As a side note, I'd like to add my voice to those who have expressed
: a desire to have access to the plain text version of the draft.  ....

A very kind soul pointed me to the html version on the net, which (with
the help of Lynx) I pulled down into a version with the tags stripped.
It's probably as close to a searchable plain-text version as we're
likely to get, so if there's general interest (email, please, including
suggestions on a preferred site) I'll zip the files (one for each
chapter/appendix) into a single archive and upload it.  I went through
and replaced some of the funky characters (hex AD was used for a hyphen)
but not all of them (the copyright symbol is pretty unrecognizable).
Speaking of copyright, please email me if you know of any restrictions
which would make it illegal to upload the files.

--
/*----------------------------------------------------------------------*/
/* Bob Kline                                       Stream International */
/* bob_kline@stream.com               formerly Corporate Software, Inc. */
/* voice: (703) 522-0820 x-311                      fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/





Author: bkline%occs.nlm.nih.gov (Bob Kline Phoenix Contract)
Date: 1995/05/22
Raw View
In section 10.3, paragraph 5, in the draft submitted for public review,
there is an example given which has me baffled.  Here it is:

  class B {};
  class D : private B { friend class Derived; };
  struct Base {
    virtual void vf1();
    virtual void vf2();
    virtual void vf3();
    virtual B*   vf4();
    void f();
  };

  struct No_good : public Base {
    D* vf4();       // error: B (base class of D) inaccessible <------*!!
  };

  struct Derived : public Base {
    void vf1();     // virtual and overrides Base::vf1()
    void vf2(int);  // not virtual, hides Base::vf2()
    char vf3();     // error: invalid difference in return type only
    D* vf4();       // okay: returns pointer to derived class  <------*!!
    void f();
  };

  ....

Could someone please help me understand why the function vf4() is legal
in Derived and not in No_good?  They look like the same situation to me.

As a side note, I'd like to add my voice to those who have expressed
a desire to have access to the plain text version of the draft.  I very
much appreciate having the PostScript version made available on the net,
but quoting passages (as above) would be much less tedious and prone
to errors if we didn't have to type them in by hand.  It would also
make searching for strings much easier, which would come in particularly
handy with some of the terms which are not explained at first appearance
(did anyone else notice how far you have to read into the document
before an explanation of the quaint "POD" appears?).  I saw the
article which pointed out that the drawback of providing the plain
text version is that it makes citation of passages from the document
ambiguous, since pagination differs from one format to another.  I
don't believe this is a legitimate line of reasoning, since a much
finer level of granularity than page number is provided in the numbering
of each paragraph (which in most cases is the same as each sentence).
I'd be very much surprised if the draft is not maintained as a troff
document, which makes the production of the plain text version trivial.
Even if it's not troff I'd be even more surprised if it isn't in a
format which supports plain text output.  Could we have some re-
consideration of this request, please?

--
/*----------------------------------------------------------------------*/
/* Bob Kline                                       Stream International */
/* bob_kline@stream.com               formerly Corporate Software, Inc. */
/* voice: (703) 522-0820 x-311                      fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/05/22
Raw View
In article 1326@nlm.nih.gov, bkline%occs.nlm.nih.gov (Bob Kline Phoenix Contract) writes:
>In section 10.3, paragraph 5, in the draft submitted for public review,
>there is an example given which has me baffled.  Here it is:
>
>  class B {};
>  class D : private B { friend class Derived; };
>  struct Base {
>    virtual void vf1();
>    virtual void vf2();
>    virtual void vf3();
>    virtual B*   vf4();
>    void f();
>  };
>
>  struct No_good : public Base {
>    D* vf4();       // error: B (base class of D) inaccessible <------*!!
>  };
>
>  struct Derived : public Base {
>    void vf1();     // virtual and overrides Base::vf1()
>    void vf2(int);  // not virtual, hides Base::vf2()
>    char vf3();     // error: invalid difference in return type only
>    D* vf4();       // okay: returns pointer to derived class  <------*!!
>    void f();
>  };
>
>  ....
>
>Could someone please help me understand why the function vf4() is legal
>in Derived and not in No_good?  They look like the same situation to me.

They are not the same. The difference is the accessibility.

No_good::vf4() returns a D*, but in Base, No_good's base class, the
overridden virtual returns a B*, which is not accessible in No_good.

Derived::vf4() returns a D*, but Derived is a friend of D, and thus
D's base-class B is accessible in Derived.

>  I very
>much appreciate having the PostScript version made available on the net,
>but quoting passages (as above) would be much less tedious and prone
>to errors if we didn't have to type them in by hand.

HTML and PDF (Acrobat) versions are available on several sites, and
their locations have been published here. I don't have a list of the
public sites handy, sorry.

---
Steve Clamage, stephen.clamage@eng.sun.com