473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Copy constructors


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::Derived() : p_(new DerivedPrivate()) {}

Derived::Derived(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!

Oct 18 '07 #1
9 10256
ke***@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::Derived() : p_(new DerivedPrivate()) {}

Derived::Derived(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::Derived()
: Base()
, p_(new DerivedPrivate())
{}
Oct 18 '07 #2
On 18 Oct, 16:06, Barry <dhb2...@gmail.comwrote:
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::Derived()
: 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...

Oct 18 '07 #3
ke***@bytebrothers.co.uk wrote in news:1192721104.884316.307060
@k35g2000prh.googlegroups.com:
On 18 Oct, 16:06, Barry <dhb2...@gmail.comwrote:
>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::Derived()
: 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
Oct 18 '07 #4
On Oct 18, 11:25 pm, ke...@bytebrothers.co.uk wrote:
On 18 Oct, 16:06, Barry <dhb2...@gmail.comwrote:


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::Derived()
: 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

Oct 18 '07 #5
On 18 Oct, 16:37, Joe Greer <jgr...@doubletake.comwrote:
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?!

Oct 18 '07 #6
On 2007-10-18 17:02, ke***@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::Derived() : p_(new DerivedPrivate()) {}

Derived::Derived(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::Derived(const Derived& s)
: Base(),
p_(new DerivedPrivate(*(s.p))) // Instead of assignment in the body
{}

--
Erik Wikström
Oct 18 '07 #7
ke***@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::Derived() : p_(new DerivedPrivate()) {}
::
:: Derived::Derived(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::Derived(const Derived& s) : Base(s), p_(new
DerivedPrivate(*(s.p_)))
{ }
Bo Persson


Oct 18 '07 #8
On 2007-10-18 11:57:22 -0400, ke***@bytebrothers.co.uk said:
>
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?!
Yes, except that you don't need to explicitly mention Base's
constructor if you want the default constructor. That's the one that
the compiler will use if you don't mention Base's constructor at all.
Busybody compilers might give you a warning for doing that, but the
behavior is well defined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 18 '07 #9
On Oct 18, 5:02 pm, ke...@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.
No you don't. Every class has a copy constructor declared.
Always. The most you can do is to make it private, and not
provide an implementation.
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::Derived() : p_(new DerivedPrivate()) {}
Derived::Derived(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?
The only explination I can see is that the author of this
warning message doesn't understand C++. You're Derived copy
constructor calls (and should call) the default constructor of
the Base class, which is probably what it should do (if e.g. the
Base class has no data members). At the very least, the warning
is only relevant if the Base class has data members.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.