Edit
Attach
Printable
topic end
<!-- * Set TOPICTITLE = Claus Brod: Address of the union (11 Jan 2006) --> <style type="text/css"> pre {background-color:#ffeecc;} </style> %STARTINCLUDE% <a name="11"></a> ---+++ [[BlogOnSoftware20060111][Address of the union]] (11 Jan 2006) The following C++ code will be rejected by both Visual C++ and gcc: <pre> class BOX { public: BOX() {} }; union { void *pointers[8]; BOX box; }; </pre> gcc says something like "error: member `BOX <anonymous union>::box' with constructor not allowed in union"; Visual C++ reports a compiler error [[http://msdn.microsoft.com/library/en-us/vccore/html/C2620.asp][C2620]]. Now that is too bad, because in my particular case, I _really_ needed both a union (to save memory in a critical area of the code) _and_ that =box= member with a default constructor! Now I'm sure that all those CEOs around the world who are currently sacking people in the thousands would readily agree that union members aren't constructive enough, but why even turn this into a C++ language rule? :D I have a workaround for this now, but I'm still a little puzzled about the compiler restriction. My guess is that the compiler is trying to avoid intialization ambiguity in a scenario like this: <pre> class FOO { int foo; public: FOO() : foo(42) {} }; class BAR { int bar; public: BAR() : bar(4711) {} }; union { FOO foo; BAR bar; }; </pre> Which constructor "wins" here? But then, C++ isn't exactly over-protective in other areas, either, so if I want to shoot myself into the foot, get out of my way, please. Or is there another reason? Hints most welcome. %COMMENT{type="below" nonotify="on"}% --- %STOPINCLUDE%
to top
End of topic
Skip to action links
|
Back to top
Edit
|
Attach image or document
|
Printable version
|
Raw text
|
Refresh
|
More topic actions
Revisions: | r1.1
|
Total page history
|
Backlinks
You are here:
Blog
>
BlogOnSoftware20060111
r1.1 - 17 Feb 2006 - 20:56 -
ClausBrod
to top
Blog
This site
2017
:
12
-
11
-
10
2016
:
10
-
7
-
3
2015
:
11
-
10
-
9
-
4
-
1
2014
:
5
2013
:
9
-
8
-
7
-
6
-
5
2012
:
2
-
10
2011
:
1
-
8
-
9
-
10
-
12
2010
:
11
-
10
-
9
-
4
2009
:
11
-
9
-
8
-
7
-
6
-
5
-
4
-
3
2008
:
5
-
4
-
3
-
1
2007:
12
-
8
-
7
-
6
-
5
-
4
-
3
-
1
2006:
4
-
3
-
2
-
1
2005:
12
-
6
-
5
-
4
2004:
12
-
11
-
10
C++
CoCreate Modeling
COM & .NET
Java
Mac
Lisp
OpenSource
Scripting
Windows
Stuff
Changes
Index
Search
Maintenance
Impressum
Datenschutzerklärung
Home
Webs
Atari
Blog
Claus
CoCreateModeling
Klassentreffen
Main
Sandbox
Sommelier
TWiki
Xplm
Jump:
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback