home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!Germany.EU.net!unido!ixos!peterg
- From: peterg@ixos.de (Peter Ginzinger)
- Newsgroups: comp.lang.c++
- Subject: C++: Problems with inheritence and virtual member functions
- Keywords: Inheritence, virtual member functions
- Message-ID: <1444@ixos.de>
- Date: 28 Jul 92 08:11:49 GMT
- Organization: iXOS Software, Munich, West Germany
- Lines: 39
-
-
- Hi all,
-
- I am a C++ - novice and have the following problem (inheritence and
- virtual functions):
-
- Scenario:
- - Baseclass X for classes X1,....,Xn
- - Baseclass N for classes N1,....,Nm
- - Memberfunctions of Xi out of {r(N1&),...,r(Nm&)} (subset) where
- r(Nj&) of Xk is different to r(Nj&) of Xl.
- - X1-,..,Xn-objects are collected in an heterogeneous list (I want to
- use the USL library class List with homogeneous lists with
- listobjects *x (of type X)).
- - If there is no r(Nj&) for Xi, then r(N&) of X should be called.
-
- Wish:
- - Defining a virtual member function of X: r(N&)
- - Traverse (Iterate) the list of Xi-objects with a Nj:
- -> a Xk-object in the list should call "his" r(Nj&) memberfunction
- (the problem is (simplified):
- X1 x1; N1 n1; // for example; X1::r(N1&) is defined
- X *x = new X1;
- x = &x1;
- x.r(n1); // -> X::r(N&) and not X1::r(N1&) is called!)
-
- I found two solutions with which I was not very happy:
- 1. No inheritence of N and defining only r(N&) for Xi and
- to analyze N with switch (N.type) { case ... } (C-like)
- 2. Define virtual memberfunctions r(N1&),...,r(Nm&) of X, which
- blows up X!
-
- Are there any other (better) solutions, or how can this general
- problem can be solved in C++ with little effort?
-
-
- Thanks,
- Peter
- (peterg@ixos.de)
-