|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? Thx! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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()) {} |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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... |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
keith@bytebrothers.co.uk wrote in news:1192721104.884316.307060
@k35g2000prh.googlegroups.com: > 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... > Don't read too much into the syntax. This basically just tells the compiler which constructor to use for the base class and provides the opportunity to provide parameters to the base class' constructor. By the time your Derived member initialization occurs and your constructor body is executed, the base class will have been initialized. joe |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 18 Oct, 16:37, Joe Greer <jgr...@doubletake.com> wrote:
> ke...@bytebrothers.co.uk wrote in news:1192721104.884316.307060 > @k35g2000prh.googlegroups.com: > > > 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... > > Don't read too much into the syntax. This basically just tells the > compiler which constructor to use for the base class and provides the > opportunity to provide parameters to the base class' constructor. By the > time your Derived member initialization occurs and your constructor body is > executed, the base class will have been initialized. Ahhh... My understanding was _nearly_ correct; By the time we reach Derived's constructor's BODY, Base is guaranteed to have been fully constructed. But while we are still in the initialisation list, we can (and apparently should) explicitly call Base's constructor. Have I got it right now?! |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 is just a warning that it is very unusual to have a copy constructor that does not call the copy constructor of the base class. You default construct the base and then copy the derived class. More idiomatic would be: Derived: erived(const Derived& s) : Base(s), p_(newDerivedPrivate(*(s.p_))) { } Bo Persson |
|
![]() |
| Outils de la discussion | |
|
|