🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.mac.programmer.codewarrior
5 messages
5 total messages Started by Gilles Serasset Thu, 03 Aug 1995 15:49
C++ hierarchy problem
#3
Author: Gilles Serasset
Date: Thu, 03 Aug 1995 15:49
53 lines
2307 bytes
Hi all,

I face a problem related to C++ classes. And I would like to know how to
get rid of it.

I defined 2 classes that know each others lets call the A1 and B1.
A1 has a slot (bb) that contains a B1
B1 has a slot (aa) that contains an A1.

Up to now, everythings fine. Now, I would like to expend the
functionality of the classes by sub-classing A1 and B1.

A2 inherits from A1.
A2 has one more method (DoThis) that wasn't defined in A1
The aa slot of an A2 instance always contains a B2 instance.

B2 inherits from B1
B2 has one more method (Dothat) that wasn't defined in B1
The aa slot of an A2 instance always contains a B2 instance.

in A2::Dothis(), I call the method DoThat for the B2 that is in the bb
slot:
void A2::DoThis(void) { (this->bb)->DoThat(); }

This works only if I cast (this->bb) to a B2*. If I don't, my codewarrior
tells me:
Error   : 'DoThat' is not a struct/union/class member
A2.cp line xx   (this->bb)->DoThat();

Hence I have to write:
void A2::DoThis(void) { ((B2 *)(this->bb))->DoThat(); }

But now, I want to introduce new classes that behave slightly differently
than B2.

B3 inherits from B2 and redefines DoThat().

A2 is not modified (but its bb slot contains a B3 instance).

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 ?

Thanks in advance,

Gilles Serasset,

======================================================
 Au paradis, on est assis a la droite de Dieu.
 C'est normal, c'est la place du mort.    P. Desproges
-----  Gilles.Serasset@imag.fr (Gilles Serasset)  ----
GETA IMAG, UJF et CNRS, BP 53 F-38041 GRENOBLE CEDEX 9
Re: C++ hierarchy problem
#4
Author: Justin Vallon
Date: Fri, 04 Aug 1995 04:34
52 lines
1812 bytes
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.

>
>Thanks in advance,
>
>Gilles Serasset,
>
>======================================================
> Au paradis, on est assis a la droite de Dieu.
> C'est normal, c'est la place du mort.    P. Desproges
>-----  Gilles.Serasset@imag.fr (Gilles Serasset)  ----
>GETA IMAG, UJF et CNRS, BP 53 F-38041 GRENOBLE CEDEX 9

-Justin
jcv6@columbia.edu
Re: C++ hierarchy problem
#5
Author: 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"
----------------------------------------------------------------------------
Re: C++ hierarchy problem
#6
Author: Justin Chris Vallon
Date: Fri, 04 Aug 1995 20:46
18 lines
1046 bytes
On Fri, 4 Aug 1995, Richard Wesley wrote:

> 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.

If B1 has pure virtual methods, B1 cannot be instantiated.  The B1 as 
declared cannot be instantiated.

> - rmgw

-Justin
jcv6@columbia.edu
Re: C++ hierarchy problem
#7
Author: Telesis North
Date: Mon, 07 Aug 1995 17:06
19 lines
1202 bytes
In article <3vqr6h$8nj@imag.imag.fr>,
Gilles Serasset  <Gilles.Serasset@imag.fr> wrote:
>I face a problem related to C++ classes. And I would like to know how to
>get rid of it.
>
>void A2::DoThis(void) { ((B2 *)(this->bb))->DoThat(); }
>
>B3 inherits from B2 and redefines DoThat().
>A2 is not modified (but its bb slot contains a B3 instance).

If I understand your system correctly, then making B2::DoThat() virtual
ought to give you what you want. (Might as well declare A2::DoThis()
virtual as well, for symmetry if nothing else.)

cheers,

Roger Pantos                 Telesis North, Inc.           telesisnorth
Mac Software Guy             telesis@ecf.toronto.edu       (AppleLink)
I've got a Mac instead of a DOS box because I need a tool, not a hobby.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads