home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11650 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  1.7 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!unido!ixos!peterg
  2. From: peterg@ixos.de (Peter Ginzinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: C++: Problems with inheritence and virtual member functions
  5. Keywords: Inheritence, virtual member functions
  6. Message-ID: <1444@ixos.de>
  7. Date: 28 Jul 92 08:11:49 GMT
  8. Organization: iXOS Software, Munich, West Germany
  9. Lines: 39
  10.  
  11.  
  12. Hi all,
  13.  
  14. I am a C++ - novice and have the following problem (inheritence and 
  15. virtual functions):
  16.  
  17.  Scenario:
  18.   - Baseclass X for classes X1,....,Xn
  19.   - Baseclass N for classes N1,....,Nm
  20.   - Memberfunctions of Xi out of {r(N1&),...,r(Nm&)} (subset) where
  21.     r(Nj&) of Xk is different to r(Nj&) of Xl.
  22.   - X1-,..,Xn-objects are collected in an heterogeneous list (I want to 
  23.     use the USL library class List with homogeneous lists with 
  24.     listobjects *x (of type X)).
  25.   - If there is no r(Nj&) for Xi, then r(N&) of X should be called. 
  26.  
  27.  Wish:
  28.   - Defining a virtual member function of X: r(N&) 
  29.   - Traverse (Iterate) the list of Xi-objects with a Nj:
  30.     -> a Xk-object in the list should call "his" r(Nj&) memberfunction
  31.     (the problem is (simplified): 
  32.       X1 x1; N1 n1; // for example; X1::r(N1&) is defined  
  33.       X *x = new X1; 
  34.       x = &x1;
  35.       x.r(n1); // -> X::r(N&) and not X1::r(N1&) is called!)
  36.  
  37.  I found two solutions with which I was not very happy:
  38.   1. No inheritence of N and defining only r(N&) for Xi and
  39.      to analyze N with switch (N.type) { case ... } (C-like)
  40.   2. Define virtual memberfunctions r(N1&),...,r(Nm&) of X, which 
  41.      blows up X!
  42.  
  43.  Are there any other (better) solutions, or how can this general 
  44.  problem can be solved in C++ with little effort?
  45.  
  46.  
  47. Thanks, 
  48. Peter
  49.  (peterg@ixos.de) 
  50.