unions

struct in union initialize

风格不统一 提交于 2019-12-13 07:58:35
问题 Currently, I have a struct in a union. For example, Struct foo{ Union u{ Struct s1{ int i1; } ss1; Struct s2{ int i2; } ss2; } wrap; }; So when I want to initialize the union, I tried to do like this. foo f = {}; f.u.ss1 = { .i1 = 0; } But the error shows no match for operator = (operand types and braced-enclosed initializer list). So what is the right way to do the initialize? Thanks in advance. 回答1: The initialisation should be: foo f; f.wrap.ss1 = {0 /*, comma seperated values, */}; 来源:

make vector 3D derived from vector ND, need to retain field x y z

≯℡__Kan透↙ 提交于 2019-12-13 07:01:03
问题 I have a vector 3D class class Vector3D{ public: float x; float y; float z; //some functions, e.g. operator+ - * / //some 3D-specific function }; and a vector N-D class. template<int constSize> class VecFloatFix{ float database[constSize]; //some functions, e.g. operator+ - * / }; I noticed that there is code-duplication between two classes, so I think I should make Vector3D derived from VecFloatFix<3> :- class Vector3D : public VecFloatFix<3>{ //some 3D-specific function }; Everything seems

How to create a fast & huge union array without wasting memory in C?

柔情痞子 提交于 2019-12-13 05:22:40
问题 I'd like to store different datatypes in the same memory using union . This array has a fixed length and shall be accessed quickly and shall waste as little memory as possible. I will define areas in which the same datatypes are stored. So I do this: #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <inttypes.h> #define RESERVED_BYTES 1073741824 //#define RESERVED_BYTES 2147483648 typedef union { char c[RESERVED_BYTES]; //1Byte uint8_t u8[RESERVED_BYTES]; //1Byte uint16_t

proper way to swap unions?

痞子三分冷 提交于 2019-12-12 16:54:40
问题 I have a class which has a union as one of its members. I also am a big fan of the copy/swap idiom. It occurred to me that there doesn't appear to be any correct (in the sense of the standard) to swap unions! Here's the best that I could come up with: char tmp[sizeof(U)]; memcpy(tmp, &u1, sizeof(U)); memcpy(&u1, &u2, sizeof(U)); memcpy(&u2, tmp, sizeof(U)); Since unions (at least in c++03) require that all members be POD, I don't see why this wouldn't work. But it just doesn't feel right. Is

How is a struct stored in memory?

旧巷老猫 提交于 2019-12-12 15:09:22
问题 I have a struct iof_header in my code, and I determined it would be 24 bytes wide. I perform a sizeof(iof_header) and it returns 32 bytes wide. Question 1 Why is it 32 bytes wide instead of 24? Question 2 Including its members, how is a struct stored in memory? Question 3 I find any time I create one of my structs that bytes[4-8 & 20-24] are all NULL, I see this apparent in my char array. The array reads as follows {4 bytes of BASEID_Code, 4 NULL bytes, 8 bytes of zeroed padding, 4 bytes of

C++: Union containing class instances calls wrong virtual function

对着背影说爱祢 提交于 2019-12-12 12:09:51
问题 I came across a strange phenomena upon running the following code: #include <iostream> class Piece { public: class Queen; class Knight; union Any; virtual const char* name() const = 0; }; class Piece::Queen : public Piece { public: virtual const char* name() const { return "Queen"; } }; class Piece::Knight : public Piece { public: virtual const char* name() const { return "Knight"; } }; union Piece::Any { public: Any() {} Piece::Queen queen; Piece::Knight knight; }; using namespace std; int

C++ Union, Struct, Member type

﹥>﹥吖頭↗ 提交于 2019-12-12 08:12:58
问题 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 ? 回答1: 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

Memory position of elements in C/C++ union

眉间皱痕 提交于 2019-12-12 07:13:09
问题 I have a union in C like this: union AUnion { struct CharBuf { char *buf; size_t len; } charbuf; uint8_t num; double fp_num; }; My question is, can I guarantee that if given the following: union AUnion u; Then the following are true: &u == &u.num &u == &u.fp_num &u == &u.charbuf I.e they all start at the beginning of the memory segment where u is stored. In the case of this C program compiled with gcc version 5.3.0 and -std=c11 the above is true: #include <stdio.h> #include <stdint.h>

Can anyone help me with the explanation of the processing of this snippet of code

落爺英雄遲暮 提交于 2019-12-12 06:58:37
问题 Actually i compiled this in a online c compiler, the output of the code was 5... how did the processing take place?? #include <stdio.h> int main() { struct ab {char a,b;}; union abcd { int c; struct ab d; }k; k.d.a=5; k.d.b=0; printf("%d",k.c); } 回答1: you have an union between an integer and a structure containing 2 chars. The code is changing the first char of the structure. Because of the union, it affects the first byte of the other union member, which is the integer. On a little-endian

Binary representation with union

左心房为你撑大大i 提交于 2019-12-12 05:05:13
问题 In the below program: union { int i; float f; } u; Assuming 32 bit compiler, u is allocated with 4 bytes in memory. u.f = 3.14159f; 3.14159f is represented using IEEE 754, in those 4 bytes. printf("As integer: %08x\n", u.i); What does u.i represent here? Is IEEE 754 binary representation interpreted as 4 byte signed int ? 回答1: Reading from i is implementation-defined blah blah blah. Still. On "normal" platforms where float is IEEE-754 binary32 format int is 32 bit 2's complement the