unions

Invalid union member

北战南征 提交于 2019-12-07 02:24:11
问题 Is there a way in Visual Studio to handle non-trivial unions. The following code is running fine using g++ -std=c++11 but VS complains: invalid union member -- class "Foo" has a disallowed member function The code is as follows: struct Foo { int value; Foo(int inV = 0) : value(inV) {} }; union CustomUnion { CustomUnion(Foo inF) : foo(inF) {} CustomUnion(int inB) : bar(inB) {} int bar; Foo foo; }; int main() { CustomUnion u(3); return 0; } Is there a way in Visual Studio to support this kind

Working with a union of structs in C

浪子不回头ぞ 提交于 2019-12-07 01:59:00
问题 Say I have the following types: typedef struct TYPEA { int type; char[12] id; } TYPEA; typedef struct TYPEB { int type; int value; } TYPEB; I want to use create a union of these types and 'int', so that I can access the "type" int without needing to know whether TYPEA or TYPEB is stored in the union (the value of int lets me determine which is actually stored there). I can't get the right syntax though. My union: typedef union MEMBER { int type; struct TYPEA a; struct TYPEB b; } MEMBER; The

C++ union array and vars?

人盡茶涼 提交于 2019-12-07 00:22:57
问题 There's no way to do something like this, in C++ is there? union { { Scalar x, y; } Scalar v[2]; }; Where x == v[0] and y == v[1] ? 回答1: How about union { struct { int x; int y; }; int v[2]; }; edit: union a { struct b { int first, second; } bee; int v[2]; }; Ugly, but that's more accurate 回答2: Since you are using C++ and not C, and since they are of the same types, why not just make x a reference to v[0] and y a reference to v[1] 回答3: Try this: template<class T> struct U1 { U1(); T v[2]; T&

Nameless union inside a union

三世轮回 提交于 2019-12-06 20:16:07
问题 I'm reading some code and found something like the following: typedef union { int int32; int boolean; time_t date; char *string; union { struct foo *a; struct foo *b; struct foo *c; }; } type_t; From syntax point of view, the inner union {} can be removed and having *a, *b and *c directly inside the outer union {}. So what's the purpose the namelessly embedded union? 回答1: Unnamed union/struct inside another union/struct is a feature of C11, and some compiler extensions (e.g, GCC). C11 §6.7.2

What is the point of 'protected' in a union in C++

青春壹個敷衍的年華 提交于 2019-12-06 19:43:30
问题 Is there anything that protected members or functions can be used for? You cannot inherit from a union so there are no children that can access it. Does it provide a functional use or is just there because removing it was hassle? 回答1: protected in a union becomes completely equivalent to private , but this allowance does no harm and avoids extra special case handling and extra differences between union and struct / class (which are described all together in the standard). Honestly, I think it

Common initial sequence and alignment

删除回忆录丶 提交于 2019-12-06 18:31:26
问题 While thinking of a counter-example for this question, I came up with: struct A { alignas(2) char byte; }; But if that's legal and standard-layout, is it layout-compatible to this struct B ? struct B { char byte; }; Furthermore, if we have struct A { alignas(2) char x; alignas(4) char y; }; // possible alignment, - is padding // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 // x - - - y - - - x - - - y - - - struct B { char x; char y; }; // no padding required union U { A a; B b; } u; Is

Is there any difference between structure and union if we have only one member?

梦想的初衷 提交于 2019-12-06 16:32:41
问题 I would like to know the difference between structure and union for one member data type if there is any. 回答1: In C: None. The famous "space-saving joke" #define struct union is almost not a joke. In C++98: Unions can only have POD members, non-union classes can have arbitrary members. In C++11: Unions can have arbitrary data members of object type (but not of reference type), but their use is more restricted that that of non-union classes. (Namely: a union cannot have virtual member

what is size of empty class and difference between union, structure and class in c++ ?

本秂侑毒 提交于 2019-12-06 13:50:12
问题 what is size of empty class and difference between union, structure and class in c++ ? My idea: if no static members in them, they should be same because all members are allocated on stack. If they are all empty, they are same. if they have static members, it depends the relative location of the members inside them. right ? thanks 回答1: C++ Standard standard specify's that the size of an Empty class should be Non-Zero . Usually, it is 1 byte on most systems. In Bjarne Stroustrup's words, the

What's the proper format for a union initializer list?

十年热恋 提交于 2019-12-06 11:56:54
Say I have a class/struct with a single union in it... something like: struct Box { union { AS128 intr; struct { AS32 a, b, c, d; }; }; }; What's the proper way to do an initializer list for this kind of data type? Box iMyBoxA = { 10, 10, 100, 100 }; Box iMyBoxB = ( Box ){ 10, 10, 100, 100 }; The 'A' option above works in many cases, but isn't completely portable... giving the error "no known conversion for argument 1 from 'brace-enclosed initializer list'". And the second doesn't compile with "Cannot omit braces around initialization of subobject..." I've tried a few other cases and can't

C - Using an union, allocating memory

限于喜欢 提交于 2019-12-06 09:03:18
I have a C structure that looks like this typedef struct event_queue{ Event* event; int size; int front; int count; int delay; } event_queue; It's a basic circular queue. The event value is an array of EventPointers, and it's traversed every X time to dequeue one of the events. It's initialized like p->event = calloc(p->size, sizeof(Event)); Thing is, I want to do a similar queue, with similar functionality, to queue other type of similar events but with slightly different data. Initially I just wanted to have separate queues and traverse them separately, but the functionality is so repeated,