Discussion:
Copy constructor and fixed size arrays.
(too old to reply)
David Webber
2009-08-12 22:13:18 UTC
Permalink
A debate is raging elsewhere about whether a fixed size array member - eg x
in

class A
{
int x[17];
};

will be correctly copied/assigned element by element by a default copy
constructor and assignment operator. I couldn't see it explicitly
mentioned in my (old) copy of Stroustrup.

Is there a definitive answer?

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Igor Tandetnik
2009-08-12 22:41:27 UTC
Permalink
Post by David Webber
A debate is raging elsewhere about whether a fixed size array member
- eg x in
class A
{
int x[17];
};
will be correctly copied/assigned element by element by a default copy
constructor and assignment operator. I couldn't see it explicitly
mentioned in my (old) copy of Stroustrup.
Is there a definitive answer?
C++ standard:

12.8p8 The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects... Each subobject is copied in the
manner appropriate to its type:
...
- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;
...
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
David Webber
2009-08-12 23:34:54 UTC
Permalink
Post by Igor Tandetnik
12.8p8 The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects... Each subobject is copied in the
...
- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;
Thanks Igor.

Section r12.8 ("Copying class objects") of my 2nd edition of Stroustrup
(1991 - can it be *that* old? - can *I* be that old?) is a good deal less
explicit than that. I *really* must buy a new copy!

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Victor Bazarov
2009-08-13 13:12:38 UTC
Permalink
Post by David Webber
Post by Igor Tandetnik
12.8p8 The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects... Each subobject is copied in the
...
- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;
Thanks Igor.
Section r12.8 ("Copying class objects") of my 2nd edition of Stroustrup
(1991 - can it be *that* old? - can *I* be that old?) is a good deal
less explicit than that.
1991 is seven years (!) before the first Standard. And five years (!)
before the last officially published draft of the first Standard. I can
imagine that it's less explicit because the language hadn't got enough
exposure at that time.
Post by David Webber
I *really* must buy a new copy!
Nah!... You've waited so long, why bother now? Just gonna be a few
short months and you can get the newest Standard once it's approved and
published. No more than a year and a half... Honest...<g>

Seriously, though, you could probably get by with the draft I mentioned
(1996, links can be found everywhere) or the current draft (see the
Committee page: http://www.open-std.org/jtc1/sc22/wg21/).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
David Webber
2009-08-13 16:12:38 UTC
Permalink
Post by Victor Bazarov
Post by David Webber
Section r12.8 ("Copying class objects") of my 2nd edition of Stroustrup
(1991 - can it be *that* old? - can *I* be that old?) is a good deal
less explicit than that.
1991 is seven years (!) before the first Standard. And five years (!)
before the last officially published draft of the first Standard. I can
imagine that it's less explicit because the language hadn't got enough
exposure at that time.
Not my fault guv. I'd already been writing C++ for a few years before I
decided that any serious C++ developer had to have a copy of Stroustrup.
It seems like only yesterday when I bought it, and now it seems I have to
buy *another* copy of a book I already have! What a rip off! :-)
Post by Victor Bazarov
Nah!... You've waited so long, why bother now? Just gonna be a few short
months and you can get the newest Standard once it's approved and
published. No more than a year and a half... Honest...<g>
The blink of an eye then. :-)
Post by Victor Bazarov
Seriously, though, you could probably get by with the draft I mentioned
(1996, links can be found everywhere) or the current draft (see the
Committee page: http://www.open-std.org/jtc1/sc22/wg21/).
Ahh, that's one of those "link thingies". Unfortunately I'm one of those
old fogies who doesn't regard information as "proper" unless it's in a nice
reference book sitting on the shelf next to the computer. I still have my
pretty set of Windows 3.1 manuals aligned nicely along the shelf. That
particular shelf, I admit, is somewhere at the back of the attic, but I
still consult it occasionally for the multimedia library. :-) But my
MS-DOS 6 manual is always to hand. (Still an excellent reference for
international keyboard layouts!)

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Bo Persson
2009-08-14 17:56:05 UTC
Permalink
Post by David Webber
Post by Victor Bazarov
Post by David Webber
Section r12.8 ("Copying class objects") of my 2nd edition of
Stroustrup (1991 - can it be *that* old? - can *I* be that old?)
is a good deal less explicit than that.
1991 is seven years (!) before the first Standard. And five years
(!) before the last officially published draft of the first
Standard. I can imagine that it's less explicit because the
language hadn't got enough exposure at that time.
Not my fault guv. I'd already been writing C++ for a few years
before I decided that any serious C++ developer had to have a copy
of Stroustrup. It seems like only yesterday when I bought it, and
now it seems I
have to buy *another* copy of a book I already have! What a rip
off! :-)
Post by Victor Bazarov
Nah!... You've waited so long, why bother now? Just gonna be a
few short months and you can get the newest Standard once it's
approved and published. No more than a year and a half...
Honest...<g>
The blink of an eye then. :-)
Post by Victor Bazarov
Seriously, though, you could probably get by with the draft I
mentioned (1996, links can be found everywhere) or the current
http://www.open-std.org/jtc1/sc22/wg21/).
Ahh, that's one of those "link thingies". Unfortunately I'm one
of those old fogies who doesn't regard information as "proper"
unless it's in a nice reference book sitting on the shelf next to
the computer. I still have my pretty set of Windows 3.1 manuals
aligned nicely along the shelf. That particular shelf, I admit, is
somewhere at the back of the attic, but I still consult it
occasionally for the multimedia library. :-) But my MS-DOS 6
manual is always to hand. (Still an excellent reference for
international keyboard layouts!)
Dave
If you absolutely want the dead-tree-edition (6 years old, soon to be
obsolete again :-), you can get it right here:

http://www.amazon.co.uk/Standard-Incorporating-Technical-Corrigendum-No/dp/0470846747



Bo Persson
David Webber
2009-08-17 14:15:59 UTC
Permalink
Post by Bo Persson
If you absolutely want the dead-tree-edition (6 years old, soon to be
http://www.amazon.co.uk/Standard-Incorporating-Technical-Corrigendum-No/dp/0470846747
Thanks. Looks like, in the interests of the forests, I should wait for
the next edition.

(OTOH I shouldn't feel too sorry for the trees. The carbon retained on my
bookshelves is *never* returned to the atmosphere, and so I'm doing my bit
to counter global warming.)

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Igor Tandetnik
2009-08-17 14:54:57 UTC
Permalink
Post by David Webber
(OTOH I shouldn't feel too sorry for the trees. The carbon retained
on my bookshelves is *never* returned to the atmosphere, and so I'm
doing my bit to counter global warming.)
Well, "never" is a really long time. Paper does degrade after a while.
For the sake of the planet, I suggest you dump your books into a peat
bog, or bury them in permafrost.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Loading...