Topic: explicit C(): suggestion of meaningful (and easy) extension


Author: cNpOp@aStPl-iAnMtSl.cPoAmM
Date: 1999/03/05
Raw View
It often happens that a derived class constructor that should invoke a
non-default base class constructor omits it, thereby silently invoking
the default base class constructor. I have repeatedly found this (e.g.
e.g. in copy constructors) when called in to track down other people's bugs.

How about saying that "explicit C()" would imply that a derived
class constructor would need to explicitly invoke the base default ctor
if that constructor is intended.

In other words,
class C
{
   int _i ;
   explicit C(){}
   C(int j): _i(j){}
};
class D: public C
{
    D():C(){}  // rather than just D(){}
    D(){}                // Would at least warn that default ctor C() will
be used
    D(int j):C(j){}    // ok
    D(int j){}          // Would at least warn that default ctor C() will be
used
};

This should be easy to implement, one would think, even if merely as a
compiler warning,
without waiting for a language spec change.

Is there any compelling reason not to do this?

   David
   Due to spam, remove capitals from address below to reply and change '$'
to '@'.
   cNpOp$aStPl-iAnMtSl.cPoAmM
---
[ 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              ]