unions

C++ Union, Struct, Member type

孤者浪人 提交于 2019-12-03 16:11:18
If I have a class: class Odp { int i; int b; union { long f; struct { WCHAR* pwszFoo; HRESULT hr; }; }; } Union means that, of all values listed, it can only take on one of those values at a time? How does that work in terms of accessing these variables? How would I access hr directly? If I set hr , what happens if I try to access f ? This is a very fraught area in the C++ standard - basically a union instance, per the standard can only be treated at any one time as if it contained one "active" member - the last one written to it. So: union U { int a; char c; }; then: U u; u.a = 1; int n = u.a

Union in c++ are they feasible

戏子无情 提交于 2019-12-03 14:56:55
问题 Can a union in C++ have a member function? How do union with data members and member functions exist if an object is created? If I suppose yes, then are they feasible any where. If yes then where? 回答1: 9.5/1 A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial constructor (12.1), a non-trivial copy constructor (12.8),

generically convert from boost::variant<T> to type

走远了吗. 提交于 2019-12-03 14:20:30
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I use to store different types of values in a struct. Only one of the specific type is ever going to be stored in that struct, however I have a vector of these structs which I need to go through and get the actual type out of the variant. Now when I need to do the conversion the types out of this variant I do this: variant second = mystruct.variant; if (second.which() == 5) //string { std::string val = boost::get<std::string>(second); modvalue->AddNodeAttribute(key, val); } else

Is accessing bytes of a __m128 variable via union legal?

只愿长相守 提交于 2019-12-03 11:31:30
Consider this variable declaration: union { struct { float x, y, z, padding; } components; __m128 sse; } _data; My idea is to assign the value through x , y , z fields, perform SSE2 computations and read the result through x , y , z . I have slight doubts as to whether it is legal, though. My concern is alignment: MSDN says that __m128 variables are automatically aligned to 16 byte boundary, and I wonder if my union can break this behavior. Are there any other pitfalls to consider here? The union's alignment should be fine, but in the case of Windows you may be able to access the 32 bit

Unions in C++11: default constructor seems to be deleted

江枫思渺然 提交于 2019-12-03 11:28:41
I am trying to understand how unions were extended by C++11. One thing that changed is the ability to use now non-static data members with non-trivial special member functions. From cppreference.com If a union contains a non-static data member with a non-trivial special member function (default constructor, copy/move constructor, copy/move assignment, or destructor), that function is deleted by default in the union and needs to be defined explicitly by the programmer. At most one data member can have a default member initializer. I am trying the following code: struct X { ~X() {}; }; union U {

How to initialize a union? [duplicate]

拟墨画扇 提交于 2019-12-03 10:18:20
This question already has an answer here: Can a union be initialized in the declaration? 3 answers If it's a struct then it can be done *p = {var1, var2..}; But seems this doesn't work with union : union Ptrlist { Ptrlist *next; State *s; }; Ptrlist *l; l = allocate_space(); *l = {NULL}; Only to get: expected expression before ‘{’ token Crashworks In C99, you can use a d esignated union initializer : union { char birthday[9]; int age; float weight; } people = { .age = 14 }; In C++, unions can have constructors . In C89, you have to do it explicitly. typedef union { int x; float y; void *z; }

Strange Behaviour Class Objects Inside Union

那年仲夏 提交于 2019-12-03 08:52:55
Hi I wanted know the reasons of the following code void main() { class test { public: test(){} int k; }; class test1 { public: test1(){} int k; }; union Test { test t1; test1 t2; }; } For the Above code it gives error " error C2620: union 'Test' : member 't1' has user-defined constructor or non-trivial default constructor " class test { public: //test(){} int k; }; class test1 { public: //test()1{}; int k; }; union Test { test t1; test1 t2; }; For the Above, No Errors. I wanted to know the Reasons. Thank you in Advance. :) According to the C++ standard (§9.5.1, cited as well in other answers):

Is it possible to put several objects together inside a union?

柔情痞子 提交于 2019-12-03 06:58:00
What if I have this: union{ vector<int> intVec ; vector<float> floatVec ; vector<double> doubleVec ; } ; Of course, I'll be using just one of the 3 vectors. But... what happens when all the 3 vectors are contructed?? Would the consructors of the 3 vectors interfere with each other?? (since the 3 of them are in the same memory address) Thanks. Current C++ standard does not allow non-POD types inside unions. You will get this compiler error from gcc : error: member ‘std::vector<int, std::allocator<int> > <anonymous union>::i’ with constructor not allowed in union error: member ‘std::vector<int,

Unions as Base Class

孤街醉人 提交于 2019-12-03 06:30:42
问题 The standard defines that Unions cannot be used as Base class, but is there any specific reasoning for this? As far as I understand Unions can have constructors, destructors, also member variables, and methods to operate on those varibales. In short a Union can encapsulate a datatype and state which might be accessed through member functions. Thus it in most common terms qualifies for being a class and if it can act as a class then why is it restricted from acting as a base class? Edit:

Unions versus structures in C

空扰寡人 提交于 2019-12-03 06:18:56
问题 The idea behind this question is to understand the deeper concepts of using union and using it in different way so as to save memory.. My question to all is-- let's say there is a structure struct strt { float f; char c; int a; } and the same structure represented in union union unin { float f; char c; int a; } If I allocate values to structure members one after another and then print them, it gets printed. But in case of union it doesn't happen, some overwriting is being done.. So I need to