🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

C++ hierarchy problem

#3
From: 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
Technical Details
Message-ID: <3vqr6h$8nj@imag.imag.fr>
Path: arc1-ca.pugleaf.net!news.imag.fr!usenet

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.imag.fr!usenet
From: Gilles Serasset <Gilles.Serasset@imag.fr>
Newsgroups: comp.sys.mac.programmer.codewarrior,comp.sys.mac.oop.misc,comp.lang.c++
Subject: C++ hierarchy problem
Date: 3 Aug 1995 15:49:37 GMT
Organization: GETA-IMAG (UJF & CNRS)
Distribution: world
Message-ID: <3vqr6h$8nj@imag.imag.fr>
NNTP-Posting-Host: pisang.imag.fr
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: Nuntius 2.0.4_68K
X-XXDate: Thu, 3 Aug 1995 16:55:03 GMT