unions

c++11 union contains data member with virtual function

て烟熏妆下的殇ゞ 提交于 2020-04-11 05:43:29
问题 #include <iostream> class derive1{ public: derive1() = default; ~derive1() = default; virtual void func() { std::cout << "derive 1" << std::endl; } }; class derive2 { public: derive2() = default; ~derive2() = default; virtual void func() { std::cout << "derice 2" << std::endl; } }; union classUnion { classUnion() {}; ~classUnion() {}; derive1 obj1; derive2 obj2; }; int main() { classUnion u1; u1.obj1.func(); // <-- OK print 'derive 1' derive1 &dev1 = u1.obj1; dev1.func(); // <-- OK print

c++11 union contains data member with virtual function

左心房为你撑大大i 提交于 2020-04-11 05:43:26
问题 #include <iostream> class derive1{ public: derive1() = default; ~derive1() = default; virtual void func() { std::cout << "derive 1" << std::endl; } }; class derive2 { public: derive2() = default; ~derive2() = default; virtual void func() { std::cout << "derice 2" << std::endl; } }; union classUnion { classUnion() {}; ~classUnion() {}; derive1 obj1; derive2 obj2; }; int main() { classUnion u1; u1.obj1.func(); // <-- OK print 'derive 1' derive1 &dev1 = u1.obj1; dev1.func(); // <-- OK print

Can a trivial type class be copied when not all its members are initialized?

笑着哭i 提交于 2020-03-18 09:42:31
问题 (I just realized I first need to solve a much more basic issue with copying unions: When a union object is copied, is a member subobject created?. Please see that other question first.) The implicitly generated copy operations (constructor and assignment) of a class perform member by member copy (initialization or assignment). (For a trivial type these are the same.) So a class with some members not initialized cannot be copied, as accessing uninitialized objects is illegal. struct C { int m1

How to access a struct member inside a union in C?

柔情痞子 提交于 2020-03-17 11:03:25
问题 I have the following union: union employee { char key; struct manager { short int age; float shares; short int level; }; struct worker { short int age; short int skill; short int department; }; } company[10]; How can I access a member of a structure which is inside the union employee ? I tried to access the age member of the manager structure this way: company[i].manager.age But I get error C2039: 'manager' : is not a member of 'employee' . 回答1: Add something after the tag declaration.

How does the caller know when there's a Decimal inside a VARIANT?

岁酱吖の 提交于 2020-02-04 01:18:05
问题 The COM VARIANT type is defined using the tagVARIANT structure like this: typedef struct tagVARIANT { union { struct { VARTYPE vt; WORD wReserved1; WORD wReserved2; WORD wReserved3; union { LONGLONG llVal; LONG lVal; BYTE bVal; SHORT iVal; FLOAT fltVal; DOUBLE dblVal; VARIANT_BOOL boolVal; VARIANT_BOOL __OBSOLETE__VARIANT_BOOL; SCODE scode; CY cyVal; DATE date; BSTR bstrVal; IUnknown *punkVal; IDispatch *pdispVal; SAFEARRAY *parray; BYTE *pbVal; SHORT *piVal; LONG *plVal; LONGLONG *pllVal;

C++ struct/union into C# struct

你说的曾经没有我的故事 提交于 2020-01-25 21:37:10
问题 How do I convert this struct/union from C++ code into my C#-UWP-code? The important thing is, that the logic and references does not change because this struct must be sent to a server. the difference to this article ( Convert C++ struct to C# ) i have got nonprimitive datatypes (as another struct and long[]) in my struct i have got unions in my struct typedef struct _HEADER { _HEADER_TYPE HeaderType; ULONG cc; union { struct { LONG Protocol; _TYPE CType; _INFO InfoDesired; // -> that's

Unions that contain a“type” member

◇◆丶佛笑我妖孽 提交于 2020-01-22 19:53:04
问题 I have a question about something I still don't understand about unions. I've read about a lot of their uses and for the most part can see how they can be useful and understand them. I've seen that they can provide a primitive "C style" polymorphism. The example of this that I have seen on a couple websites is SDL's event union: typedef union { Uint8 type; SDL_ActiveEvent active; SDL_KeyboardEvent key; SDL_MouseMotionEvent motion; SDL_MouseButtonEvent button; SDL_JoyAxisEvent jaxis; SDL

C Unions output unclear

家住魔仙堡 提交于 2020-01-21 10:20:13
问题 I got some troubles understanding unions and how they work. #include <stdio.h> union date { int year; char month; char day; }; int main() { union date birth; birth.year = 1984; birth.month = 7; birth.day = 28; printf("%d, %d, %d\n", birth.year, birth.month, birth.day); return 0; } so since it's an union it will give me 4 bytes, because int is the highest type given in this union. that's all I got from reading and I dont know why the output is 1820, 28, 28 回答1: Unions in C use same memory

Error: copy assignment operator not allowed in union

依然范特西╮ 提交于 2020-01-14 09:38:20
问题 I am compiling the code below when the following erro comes up. I am unable to find the reason. typedef union { struct { const int j; } tag; } X; int main(){ return 0; } error: member `<`anonymous union>::`<`anonymous struct> `<`anonymous union>::tag with copy assignment operator not allowed in union This code compiles fines with gcc though. Gives error only with g++. 回答1: In order to have a member of a union of some class type T , T 's special member functions (the default constructor, copy

Shapeless: map from coproduct to different coproduct

北战南征 提交于 2020-01-13 07:59:09
问题 In the following, I'm trying to make a polymorphic function to convert a RawFeatureValue into a RefinedFeatureValue . import shapeless._ object test { type RawFeatureValue = Int :+: Double :+: String :+: CNil type RefinedFeatureValue = Int :+: Double :+: CNil private object convert extends Poly1 { implicit def caseInt = at[Int](i => i) implicit def caseDouble = at[Double](d => d) implicit def caseString = at[String](s => s.hashCode) } val a = Coproduct[RawFeatureValue](12) val b: