🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.codewarrior
Article #5

Re: C++ hierarchy problem

#5
From: Richard Wesley
Date: Fri, 04 Aug 1995 15:04
58 lines
2353 bytes
jcv6@columbia.edu (Justin Vallon) wrote:
>In article <3vqr6h$8nj@imag.imag.fr>, Gilles Serasset
><Gilles.Serasset@imag.fr> wrote:
>
>>Hi all,
>>
>...
>>B3 inherits from B2 and redefines DoThat().
>...
>>When I call A2::DoThis(), B2::DoThat() is called because of the casting.
>>And I would like B3::DoThat() to be called instead.
>>
>>How can I do this ? Am I missing something in C++ hierarchies principles ?
>
>If you want B3::DoThat() to 'override' B2::DoThat(), then declare DoThat
>as virtual.
>
>class B2 : public B1 {
>   ...
>   virtual void DoThat(void);
>   ...
>};
>
>class B3 : public B2 {
>   ...
>   virtual void DoThat(void);
>   ...
>};
>
>Now, when you have a B3 being pointed to by a B2 *, B3::DoThat is called.
>
>void foo(void) {
>   B2 *pb2 = new B3;
>   pb2->DoThat();  /* Calls B3::DoThat(), since pb2 is 'really' a B3.  */
>};
>
>It seems like you will need the cast in A2, unless you could declare
>virtual DoThat in B1, and then A1 or A2 would be able to call
>bb->DoThat(), since B1::DoThat() is virtual and defined in B1.

You should also think about having

class B1 {

virtual void DoThat (void) = 0;
};

If you never make a B1 this will allow you to make a B1.

- rmgw

http://www.punchdeck.com/hawkfish/PunchDeck.html

----------------------------------------------------------------------------
Richard Wesley  hawkfish@punchdeck.com | "'Hand it round first, and cut it
Punch Deck Consulting pnchdeck@aol.com |  afterwards.'" - Lewis Carroll,
     Macintosh Software Development    |    "Through the Looking Glass"
----------------------------------------------------------------------------
Technical Details
Message-ID: <3vtcua$cn@news.halcyon.com>
Path: arc1-ca.pugleaf.net!news.halcyon.com!usenet
References: <3vqr6h$8nj@imag.imag.fr> <jcv6-0408950034570001@dialup168.cc.columbia.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.halcyon.com!usenet
From: Richard Wesley <hawkfish@punchdeck.com>
Newsgroups: comp.sys.mac.programmer.codewarrior,comp.sys.mac.oop.misc,comp.lang.c++
Subject: Re: C++ hierarchy problem
Date: 4 Aug 1995 15:04:42 GMT
Organization: Punch Deck Consulting
Message-ID: <3vtcua$cn@news.halcyon.com>
References: <3vqr6h$8nj@imag.imag.fr> <jcv6-0408950034570001@dialup168.cc.columbia.edu>
NNTP-Posting-Host: blv-pm10-ip27.halcyon.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 1.1N (Macintosh; I; 68K)
To: jcv6@columbia.edu
X-URL: news:jcv6-0408950034570001@dialup168.cc.columbia.edu