c++ - Stuff a class with user-defined constructors into a union -



c++ - Stuff a class with user-defined constructors into a union -

class foo { foo(int val) { /* initialization */ } foo() { /* nil */ } }; union bar { foo foo; };

that code generates error:

error c2620: fellow member 'bar::foo' of union 'bar' has user-defined constructor or non-trivial default constructor

i understand why you'd throw error if constructor did something, constructor here takes no parameters , nothing. there way can stuff class union? i've had resort way doing char foo[sizeof(foo)] , cleaner solution.

originally question:

http://stackoverflow.com/questions/321351/initializing-a-union-with-a-non-trivial-constructor:

from c++03, 9.5 unions, pg 162

a union can have fellow member functions (including constructors , destructors), not virtual (10.3) functions. union shall not have base of operations classes. union shall not used base of operations class.an object of class non-trivial constructor (12.1), non-trivial re-create constructor (12.8), non-trivial destructor (12.4), or non-trivial re-create assignment operator (13.5.3, 12.8) cannot fellow member of union, nor can array of such objects

so, class forbidden fellow member of union.

c++ constructor unions

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -