From -6710662833353775739 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,2f6df499df366d54 X-Google-Attributes: gidf78e5,public From: Christopher Eltschka Subject: Re: Access control Date: 1999/05/26 Message-ID: <374BE6B0.C5655E6C@physik.tu-muenchen.de>#1/1 X-Deja-AN: 482355375 X-Nntp-Posting-Host: coulomb.t30.physik.tu-muenchen.de Content-Transfer-Encoding: 7bit Approved: stephen.clamage@sun.com (comp.std.c++) References: <7hvum9$4bb$1@nnrp1.deja.com> X-UID: 0000000001 X-Status: $$$T Content-Type: text/plain; charset=us-ascii Organization: [posted via] Leibniz-Rechenzentrum, Muenchen (Germany) Mime-Version: 1.0 Newsgroups: comp.std.c++ Originator: clamage@taumet Philip Gibbs wrote: > > In article <7hvum9$4bb$1@nnrp1.deja.com>, Andrei Alexandrescu > writes > > > >Let's analyze this code: > > > >class A > >{ > > A(); > >}; > > > >class B : public A > >{ > >public: > > B(); > >}; > > > >B::B() > >{ > >} > > > >B var; > > > >The code is in error, because B's default constructor will implicitly > >call A's default constructor, which is private. > > > >The question is: when should the compiler generate an error? This is a > >no-nonsense question, please take it seriously. > > > >1. At B::B()'s declaration > >2. At B::B()'s definition > >3. At var's definition > > 2. I would expect it at B::B()'s definition because this > uses the private constructor A::A() which is illegal here. > > > > >Is there any guarantee on *where* the error will be generated? > > I don't think there even has to be a concept of where in the code > a compile time error is generated. > > >Put another way: if you comment out var's definition, will the code be > >valid? > > No, and with good reason. A similar variable declaration might > appear in another file where the definition of B::B() is not > included. Separate compilation would therefore be impossible > if the code was allowed to be valid. > > >If not, and if you comment B::B's definition, will the code be > >valid? > > Yes. The definition of B::B() could be in another source file > and could be valid if it used the copy constructor of A instead > of the default constructor. The copy constructor would have to > construct from an object of a class derived from A (e.g. B) > because objects > > I know the following is useless but it is well formed > code giving an alternative definition of B::B() which will > produce no compile time error > > B b; > B::B(): > A(b) > { > } > > I think the result must be undefined since b is constructed > using itself. I can't see a way of producing a definition > of B::B() which would not have undefined behaviour but it > would be a clever compiler which could deduce as much from > the class definitions. What about: B::B(): A(B()) { } This will lead to infinite recursion at runtime, but it will not give undefined behaviour. It's not any more useful than your version, but at least it isn't allowed to format your hard disk ;-) [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]