[英]C++ struct in union acces over union member
我有一個結構,其中包含一個由uint32_t和另一個(匿名)結構組成的Union。 看起來像這樣:
struct sMesswertNummer {
union {
uint32_t MesswertNummerFull;
struct {
uint16_t MesswertNummerPart:16;
uint16_t AggregatNummer:16;
uint8_t AggregatSub:8;
uint8_t AggregatTyp:8;
};
};
sMesswertNummer() { this->MesswertNummerFull=0; }
sMesswertNummer(uint16_t MesswertNummerPart,uint16_t AggregatNummer, uint8_t AggregatSub, uint8_t AggregatTyp) {
this->MesswertNummerFull=0;
this->MesswertNummerPart=MesswertNummerPart;
this->AggregatNummer=AggregatNummer;
this->AggregatSub=AggregatSub;
this->AggregatTyp=AggregatTyp;
}
};
我像這樣用構造函數的調用來初始化結構
MesswertNummer(0x0001,0x0002,0x30,0x02);
之后,我的變量具有正確的值, Var.MesswertNummerPart
為0x0001, Var.AggregatNummer
為0x0002,依此類推。
但是如果我調用Var.MesswertNummerFull我得到0x0001(是MesswertNummerPart)而不是0x023000020001(0x02-> AggregateTyp,0x30-> AggregationSubm 0x0002-> AggregationNummer,0x0001-> MesswertNummerPart)
我怎樣才能通過調用Var.MesswertNummerFull獲得整個號碼?
只需再次輸入即可解決問題。 MesswertNummerFull
必須是uint64_t
(16 + 16 + 8 + 8 = 48)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.