On Oct 18, 11:25 pm, ke...@bytebrothers.co.uk wrote:
> On 18 Oct, 16:06, Barry <dhb2...@gmail.com> wrote:
>
>
>
>
>
> > ke...@bytebrothers.co.uk wrote:
> > > Now this all compiles and works just fine, but when I turn on "-Wall -
> > > W" in gcc, it tells me that:
>
> > > derived.cc:134: warning: base class 'class Base' should be explicitly
> > > initialized in the copy constructor
>
> > > I'm afraid I'm being rather dense today, as I don't understand what
> > > it's complaining about. Can someone explain for me please?
>
> > It complains that you should write your Derived ctor in this way:
>
> > Derived:
erived()
> > : Base()
> > , p_(new DerivedPrivate())
> > {}
>
> OK, this makes the warning go away - thanks. Now, what's happening?
> I thought that by the time we reached Derived's constructor (in this
> case, copy constructor), Base was guaranteed to have been fully
> constructed. This result implies I am wrong...- Hide quoted text -
>
> - Show quoted text -
Hmmm... but we write such code:
class base
{
public:
base(int bn){}
~base(){}
};
class derived : public base
{
public:
derived(int dn) : base(dn){} // call base class constructor
~derived(){}
};
When it comes to the execution of derived(int dn){}, according to your
understanding, it is meaningless to call base() since base's been
constructed. But this really is what our code usually looks like.
I think by the time we reach Derived's constructor, it is just the
start to construct a Derived object, which is to say "we are going to
construct a Derived object now, and we should start from the
construction of its base".
Regards,
-robin