From 7674463203406464864 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,2f6df499df366d54 X-Google-Attributes: gidf78e5,public From: Philip Gibbs Subject: Re: Access control Date: 1999/05/20 Message-ID: #1/1 X-Deja-AN: 480314422 Approved: stephen.clamage@sun.com (comp.std.c++) References: <7hvum9$4bb$1@nnrp1.deja.com> X-UID: 0000000001 X-Status: $$$T Organization: Weburbia Mime-Version: 1.0 Newsgroups: comp.std.c++ Originator: clamage@taumet 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. [ 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 ]