home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Restatement: Virtual base class & reference initialisation
- Message-ID: <1993Jan11.190233.16950@taumet.com>
- Organization: TauMetric Corporation
- References: <AJP.93Jan11093431@dsl.eng.cam.ac.uk>
- Date: Mon, 11 Jan 1993 19:02:33 GMT
- Lines: 40
-
- ajp@dsl.eng.cam.ac.uk (A. J. Piper) writes:
-
- |My original question on this did not include an example which is probably why I
- |was misunderstood. The scenario is this:
- |
- |class A {
- | const int& a;
- | A(const int& i) : a(i) {}
- | A() : a(0) {}
- |};
- |
- |class B : virtual public A {
- | B(const int &i) : A(i) {}
- |};
- |
- |class C : public B {
- | C(const int& i) : B(i) {}
- |};
- |
- |The reason I asked is that if you create an object of type C then B's
- |initialisation of A is ignored and the default constructor is used instead ...
-
- That's correct.
-
- |So how do I initialise the reference ? Well cfront
- |3.0.1 allows me to access the constructor of A directly from the constructor
- |of C so I can write:
- |
- |class C : public B {
- | C(const int& i) : B(i), A(i) {}
- |};
- |
- |Which solves my original question.
-
- Right. This area of the language changed, and older compilers might
- not work this way. Relatively recent compilers should support this
- code, and so should all future compilers.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
-