Ary Borenszweig Wrote: > Nick Sabalausky wrote: > > "Ary Borenszweig" <[email protected]> wrote in message > > news:[email protected]... > >> Andrei Alexandrescu wrote: > >>> Walter and I just discussed the matter of inheriting constructors. Our > >>> thought on the issue: > >>> > >>> a) If a class doesn't define any constructors and adds no fields, inherit > >>> constructors. Example: > >>> > >>> class MyException : Exception {} > >>> > >>> b) If a class defines at least one constructor, do not inherit > >>> constructors. > >>> > >>> c) If a class doesn't define any constructors but does add at least a > >>> non-static field -> undecided. > >>> > >>> What do you think? > > > >> I think c should be a compile-time error. > >> > > > > Why? (Not to be contentious.) > > At first I thought you might want to add fields to a subclass for > logging, or caching, stuff like that, while still wanting to inherit the > constructors. Then I checked some code for a project I wrote in Java and > always when the subclass had new fields it defined a different > constructor, and the logging fields were static. So I think that most of > the time you'd want to inherit the constructors when you don't define > new fields.
Since class member data is default initialized, I'm not sure I agree. For example, let's say I want to modify a string class to add a search feature, and I want it to cache the prior result for performance reasons. There's no need to ctor-initialize the result field, only to make sure it's null at the start.
