home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18925 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.1 KB  |  66 lines

  1. Path: sparky!uunet!pipex!unipalm!uknet!cam-eng!dsl!ajp
  2. From: ajp@dsl.eng.cam.ac.uk (A. J. Piper)
  3. Newsgroups: comp.lang.c++
  4. Subject: Restatement: Virtual base class & reference initialisation
  5. Message-ID: <AJP.93Jan11093431@dsl.eng.cam.ac.uk>
  6. Date: 11 Jan 93 14:34:31 GMT
  7. Sender: ajp@eng.cam.ac.uk (A.J. Piper)
  8. Organization: Engineering Department, Cambridge University, England.
  9. Lines: 54
  10. Nntp-Posting-Host: dsl.eng.cam.ac.uk
  11.  
  12.  
  13.  
  14. My original question on this did not include an example which is probably why I
  15. was misunderstood. The scenario is this:
  16.  
  17. class A {
  18.         const int& a;
  19.         A(const int& i) : a(i) {}
  20.         A() : a(0) {}
  21. };
  22.  
  23. class B : virtual public A {
  24.         B(const int &i) : A(i) {}
  25. };
  26.  
  27. class C : public B {
  28.         C(const int& i) : B(i) {}
  29. };
  30.  
  31. The reason I asked is that if you create an object of type C then B's
  32. initialisation of A is ignored and the default constructor is used instead
  33. (try it - it's true) this is because B in C is not the most-derived class for
  34. the object (ARM p293-294). So how do I initialise the reference ? Well cfront
  35. 3.0.1 allows me to access the constructor of A directly from the constructor
  36. of C so I can write:
  37.  
  38. class C : public B {
  39.         C(const int& i) : B(i), A(i) {}
  40. };
  41.  
  42. Which solves my original question. However, this "feature" is none to clear in
  43. the ARM (is it legal?) and I'm a bit concerned about the behaviour of my
  44. program altering `behind my back' as it were as soon as I start deriving
  45. things. The example the ARM gives involves deriving C from A again which
  46. means the constructor is not ignored as C *is* the most-derived class for
  47. the object.
  48.  
  49. Is there any other way ? I would have thought a better solution would be to
  50. allow the original code to work - as long as the compiler can detect that
  51. constructors in a multi-path initialisation are all doing the same thing and
  52. thus ignore all but one of them.
  53.  
  54. andy
  55.  
  56. --
  57. Andy Piper                    email: ajp@uk.ac.cam.eng
  58. Cambridge University Engineering Department     tel: +44 223 33 2754
  59. Trumpington St
  60. Cambridge, UK
  61. --
  62. Andy Piper                    email: ajp@uk.ac.cam.eng
  63. Cambridge University Engineering Department     tel: +44 223 33 2754
  64. Trumpington St
  65. Cambridge, UK
  66.