Discussion: Copy constructors
Afficher un message
Vieux 18/10/2007, 18h11   #7
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copy constructors

On 2007-10-18 17:02, 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_); }


Just a question, does not DerivedPrivate have a copy-constructor? You
should be able to use something like

Derived:erived(const Derived& s)
: Base(),
p_(new DerivedPrivate(*(s.p))) // Instead of assignment in the body
{}

--
Erik Wikström
  Réponse avec citation
 
Page generated in 0,04955 seconds with 9 queries