Discussion: Copy constructors
Afficher un message
Vieux 18/10/2007, 16h06   #2
Barry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copy constructors

keith@bytebrothers.co.uk wrote:
> Hi, I've been through the FAQ-lite and can't see this mentioned, so
> here goes...
>
> I've got an abstract base class called Base which has no copy
> constructor at all. In the derived class I have something like this:
>
> // derived.h-----------------------------------------------------
> class DerivedPrivate; // Not defined here
> class Derived : public Base
> {
> private:
> class DerivedPrivate* const p_;
>
> public:
> Derived();
> Derived(const Derived& s);
> // remainder snipped
> }
> // end--------------------------------------------------------------
>
> // derived.cc----------------------------------------------------
> // definition of DerivedPrivate skipped
> Derived:erived() : p_(new DerivedPrivate()) {}
>
> Derived:erived(const Derived& s) : p_(new DerivedPrivate())
> { *p_ = *(s.p_); }
> // remainder skipped
> // end--------------------------------------------------------------
>
> 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())
{}


  Réponse avec citation
 
Page generated in 0,05073 seconds with 9 queries