473,396 Members | 1,786 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,396 software developers and data experts.

Why can a union have a member with a copy constructor?

Why can a union have a member with a copy constructor?
Sep 27 '08 #1
7 6391
On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScreen.comwrote:
Why can a union have a member with a copy constructor?
Because it's not a class. In other words, Bjarne Stroustrup says so,
as well as Dennis Ritchie, and we, paltry followers, endorse it.
Sep 27 '08 #2
Sam
Peter Olcott writes:
Why can a union have a member with a copy constructor?
How do you know when that specific union member should be constructed?

What do you think should happen when two or more union members have
constructors?

You should be able to figure out the answer to your questions, by yourself.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjdgnMACgkQx9p3GYHlUOJV8gCfVoaGXLEE2t iuLz0L3EKycujo
UC8An2pQoI7j1alIyfIIflwIzG2pcJtg
=avkm
-----END PGP SIGNATURE-----

Sep 27 '08 #3
So then for only arbitrary and capricious reasons, not
functional reasons?

"puzzlecracker" <ir*********@gmail.comwrote in message
news:03**********************************@j22g2000 hsf.googlegroups.com...
On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScreen.com>
wrote:
>Why can a union have a member with a copy constructor?

Because it's not a class. In other words, Bjarne
Stroustrup says so,
as well as Dennis Ritchie, and we, paltry followers,
endorse it.

Sep 27 '08 #4
Sam wrote:
Peter Olcott writes:
Why can a union have a member with a copy constructor?

How do you know when that specific union member should be constructed?

What do you think should happen when two or more union members have
constructors?

You should be able to figure out the answer to your questions, by yourself.
If a class includes a union the class could also include a member that
indicates which element of the union is intended. In this case I see
no reason why this class that includes a union could not have a copy
constructor.

Carrying this same idea further this single member could be the first
element of a union of structs. In this case the union itself could
directly support a copy contructor, because this first element would
always indicate which of the structs is intended.
Sep 27 '08 #5
PeteOlcott wrote:
Sam wrote:
>Peter Olcott writes:
>>Why can a union have a member with a copy constructor?
How do you know when that specific union member should be constructed?

What do you think should happen when two or more union members have
constructors?

You should be able to figure out the answer to your questions, by yourself.

If a class includes a union the class could also include a member that
indicates which element of the union is intended. In this case I see
no reason why this class that includes a union could not have a copy
constructor.
That's because there isn't one. This isn't the same as a union
including a class.
Carrying this same idea further this single member could be the first
element of a union of structs. In this case the union itself could
directly support a copy contructor, because this first element would
always indicate which of the structs is intended.
No, a union has no such concept. The first member of a union of structs
is often used to indicate which member is in use in C code. But that's
just a form of poor man's polymorphism. We don't need to use tricks
like than in C++.

--
Ian Collins.
Sep 27 '08 #6
On Sep 27, 2:43 am, puzzlecracker <ironsel2...@gmail.comwrote:
On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScreen.comwrote:
Why can a union have a member with a copy constructor?
First, the above statement is more or less wrong. An object
with a non-trivial copy constructor cannot be a member of a
union. (The next version of the standard will loosen this
restriction somewhat. And obviously, an object with a trivial
copy constructor can be a member of a union.)
Because it's not a class.
A union is a class. It's a very special type of class, but
according to the standard, it's a class.
In other words, Bjarne Stroustrup says so, as well as Dennis
Ritchie, and we, paltry followers, endorse it.
The problem is the compiler generated copy constructor. What
should it do if a member has a non-trivial copy constructor,
given that it doesn't know which member is active? The
standards (both C and C++) restricts union members to the cases
where a simple bitwise copy will work. This isn't the case for
an object with a non-trivial copy constructor.

And of course, it's no issue in C, since the copy semantics of
all types corresponds to a trivial copy in C++.

--
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
Sep 27 '08 #7
On Sep 27, 5:47 am, PeteOlcott <PeteOlc...@gmail.comwrote:
Sam wrote:
Peter Olcott writes:
Why can a union have a member with a copy constructor?
How do you know when that specific union member should be
constructed?
What do you think should happen when two or more union
members have constructors?
You should be able to figure out the answer to your
questions, by yourself.
Now that's what I call a snotty response. It was a perfectly
valid question. All the more so as the restriction is being
lifted in the next version of the standard, so it obviously
wasn't necessary.
If a class includes a union the class could also include a
member that indicates which element of the union is intended.
Only one member of a union is active at a time. What do you do
when it isn't the member which indicates which element is
active.

It would have been possible to define yet another type
(generally called a discriminated union), which would be more or
less a struct with the union and an indication of which element
is active. There was some talk about it when the (C++) standard
was being developed, but I don't think it ever got to the point
of a formal proposal. (You'd also want a possibility of
interrogating the type, etc.) The general feeling then was, I
think, that this was basically already supported by a pointer to
a base type and dynamic_cast.
In this case I see no reason why this class that includes a
union could not have a copy constructor.
As I said, the next version of the standard will allow
objects with non-trivial copy constructors as members. If the
union has such a member, however, the implicitly defined copy
constructor will be absent, and either the user provides a copy
constructor (which is legal even now), and has some means of
knowing which object is active, or the union cannot be copied.
Carrying this same idea further this single member could be
the first element of a union of structs. In this case the
union itself could directly support a copy contructor, because
this first element would always indicate which of the structs
is intended.
You're basically trying to reinvent discriminated unions. It
can certainly be done---other languages do it. But it does
require a lot of specification.

--
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
Sep 27 '08 #8

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

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
4
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for...
4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Jeff Mallett | last post by:
VC++.NET gave me Compiler Error C2621, which states, "A union member cannot have a copy constructor." Yikes, that can't be true! As I understand it, *all* class objects have copy constructors,...
24
by: rdc02271 | last post by:
Hello! Is this too crazy or not? Copy constructor: why can't I copy objects as if they were structs? I have a set of simple objects (no string properties, just integers, doubles) and I have to...
2
by: Alex Vinokur | last post by:
If a class contains a member that is a pointer, one should implement copy constructor. Should one implement copy constructor if a class contains a member that is a reference? -- Alex...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.