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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Restatement: Virtual base class & reference initialisation
  5. Message-ID: <1993Jan11.190233.16950@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <AJP.93Jan11093431@dsl.eng.cam.ac.uk>
  8. Date: Mon, 11 Jan 1993 19:02:33 GMT
  9. Lines: 40
  10.  
  11. ajp@dsl.eng.cam.ac.uk (A. J. Piper) writes:
  12.  
  13. |My original question on this did not include an example which is probably why I
  14. |was misunderstood. The scenario is this:
  15. |class A {
  16. |        const int& a;
  17. |        A(const int& i) : a(i) {}
  18. |        A() : a(0) {}
  19. |};
  20. |class B : virtual public A {
  21. |        B(const int &i) : A(i) {}
  22. |};
  23. |class C : public B {
  24. |        C(const int& i) : B(i) {}
  25. |};
  26. |The reason I asked is that if you create an object of type C then B's
  27. |initialisation of A is ignored and the default constructor is used instead ...
  28.  
  29. That's correct.
  30.  
  31. |So how do I initialise the reference ? Well cfront
  32. |3.0.1 allows me to access the constructor of A directly from the constructor
  33. |of C so I can write:
  34. |class C : public B {
  35. |        C(const int& i) : B(i), A(i) {}
  36. |};
  37. |Which solves my original question.
  38.  
  39. Right.  This area of the language changed, and older compilers might
  40. not work this way.  Relatively recent compilers should support this
  41. code, and so should all future compilers.
  42. -- 
  43.  
  44. Steve Clamage, TauMetric Corp, steve@taumet.com
  45.