473,549 Members | 4,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6402
On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScre en.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)

iEYEABECAAYFAkj dgnMACgkQx9p3GY HlUOJV8gCfVoaGX LEE2tiuLz0L3EKy cujo
UC8An2pQoI7j1al IyfIIflwIzG2pcJ tg
=avkm
-----END PGP SIGNATURE-----

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

"puzzlecrac ker" <ir*********@gm ail.comwrote in message
news:03******** *************** ***********@j22 g2000hsf.google groups.com...
On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScre en.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...@gm ail.comwrote:
On Sep 26, 8:25 pm, "Peter Olcott" <NoS...@SeeScre en.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 objektorientier ter Datenverarbeitu ng
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...@gma il.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 objektorientier ter Datenverarbeitu ng
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
21174
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 pointers) and for objects types call their default constructor. Any others points i should know?
4
3488
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 dynamically allocated member variables). Anyway, I have a program that segfaults without a copy constructor but if I add an empty one, the segfault is...
4
1797
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 assignment, should copy constructor and assignment operator be unnecessary? 2. If a base class have a pointer member, should its derived classes...
8
2967
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr...
4
7113
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, since if they aren't explicit, one is implicitly generated. If this were true, class objects could not be members of a union, but I know they can...
24
3606
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 copy the same object millions of times. So instead of writing in the copy constructor property1=SourceObject.property1 can't I use memory copy...
2
1918
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 Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
22
3596
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 v);
34
3642
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 there is a constructor that accepts the base class. This one should be used for copying. In fact i have a set of classes with a common abstract...
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7455
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7814
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.