這篇文章主要介紹“C++中怎么使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體”,在日常操作中,相信很多人在C++中怎么使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C++中怎么使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
光澤網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
A well-designed tagged union is type safe. An anonymous union simplifies the definition of a class with a (tag, union) pair.
良好設(shè)計(jì)的命名聯(lián)合體是類型安全的。無名聯(lián)合體簡(jiǎn)化了包含(標(biāo)簽,聯(lián)合體)對(duì)的類的設(shè)計(jì)。
Example(示例)
This example is mostly borrowed from TC++PL4 pp216-218. You can look there for an explanation.
這段示例代碼主要借用自TC++PL4的216頁到218頁(中文版:C++程序設(shè)計(jì)語言(原書第四版)p186-p188)。你可以查看該書中的解釋。
The code is somewhat elaborate. Handling a type with user-defined assignment and destructor is tricky. Saving programmers from having to write such code is one reason for including variant in the standard.
這段代碼有些復(fù)雜。使用用戶定義的賦值和析構(gòu)函數(shù)處理一種類型不是那么容易。把程序員從必須編寫這樣的代碼的情況中解救出來是在標(biāo)準(zhǔn)庫中增加variant的原因之一。
class Value { // two alternative representations represented as a union
private:
enum class Tag { number, text };
Tag type; // discriminant
union { // representation (note: anonymous union)
int i;
string s; // string has default constructor, copy operations, and destructor
};
public:
struct Bad_entry { }; // used for exceptions
~Value();
Value& operator=(const Value&); // necessary because of the string variant
Value(const Value&);
// ...
int number() const;
string text() const;
void set_number(int n);
void set_text(const string&);
// ...
};
int Value::number() const
{
if (type != Tag::number) throw Bad_entry{};
return i;
}
string Value::text() const
{
if (type != Tag::text) throw Bad_entry{};
return s;
}
void Value::set_number(int n)
{
if (type == Tag::text) {
s.~string(); // explicitly destroy string
type = Tag::number;
}
i = n;
}
void Value::set_text(const string& ss)
{
if (type == Tag::text)
s = ss;
else {
new(&s) string{ss}; // placement new: explicitly construct string
type = Tag::text;
}
}
Value& Value::operator=(const Value& e) // necessary because of the string variant
{
if (type == Tag::text && e.type == Tag::text) {
s = e.s; // usual string assignment
return *this;
}
if (type == Tag::text) s.~string(); // explicit destroy
switch (e.type) {
case Tag::number:
i = e.i;
break;
case Tag::text:
new(&s) string(e.s); // placement new: explicit construct
}
type = e.type;
return *this;
}
Value::~Value()
{
if (type == Tag::text) s.~string(); // explicit destroy
}
到此,關(guān)于“C++中怎么使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站名稱:C++中怎么使用匿名聯(lián)合體實(shí)現(xiàn)附帶標(biāo)簽的聯(lián)合體
本文URL:http://chinadenli.net/article22/gepecc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站改版、網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)