home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pipex!unipalm!uknet!cam-eng!dsl!ajp
- From: ajp@dsl.eng.cam.ac.uk (A. J. Piper)
- Newsgroups: comp.lang.c++
- Subject: Restatement: Virtual base class & reference initialisation
- Message-ID: <AJP.93Jan11093431@dsl.eng.cam.ac.uk>
- Date: 11 Jan 93 14:34:31 GMT
- Sender: ajp@eng.cam.ac.uk (A.J. Piper)
- Organization: Engineering Department, Cambridge University, England.
- Lines: 54
- Nntp-Posting-Host: dsl.eng.cam.ac.uk
-
-
-
- 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
- (try it - it's true) this is because B in C is not the most-derived class for
- the object (ARM p293-294). 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. However, this "feature" is none to clear in
- the ARM (is it legal?) and I'm a bit concerned about the behaviour of my
- program altering `behind my back' as it were as soon as I start deriving
- things. The example the ARM gives involves deriving C from A again which
- means the constructor is not ignored as C *is* the most-derived class for
- the object.
-
- Is there any other way ? I would have thought a better solution would be to
- allow the original code to work - as long as the compiler can detect that
- constructors in a multi-path initialisation are all doing the same thing and
- thus ignore all but one of them.
-
- andy
-
- --
- Andy Piper email: ajp@uk.ac.cam.eng
- Cambridge University Engineering Department tel: +44 223 33 2754
- Trumpington St
- Cambridge, UK
- --
- Andy Piper email: ajp@uk.ac.cam.eng
- Cambridge University Engineering Department tel: +44 223 33 2754
- Trumpington St
- Cambridge, UK
-