溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++怎么使用T*或onwer<T*>指明唯一對(duì)象

發(fā)布時(shí)間:2021-11-26 13:43:17 來源:億速云 閱讀:217 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“C++怎么使用T*或onwer<T*>指明唯一對(duì)象”,在日常操作中,相信很多人在C++怎么使用T*或onwer<T*>指明唯一對(duì)象問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”C++怎么使用T*或onwer<T*>指明唯一對(duì)象”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

F.22: Use T* or owner<T*> to designate a single object(使用T*或owner<T*>指明唯一對(duì)象)

Reason(原因)

Readability: it makes the meaning of a plain pointer clear. Enables significant tool support.

可讀性:這可以讓裸指針的含義更明確。使重要的工具支持有效。

譯者注:owner<T*>是gsl(準(zhǔn)則支持庫)提供的一個(gè)功能,從編譯的角度來看和T*的含義一致,但是附加了所有權(quán)語義,可以幫助程序員理解代碼和工具檢查。

Note(注意)

In traditional C and C++ code, plain T* is used for many weakly-related purposes, such as:

在傳統(tǒng)的C和C++代碼中,裸指針用于很多沒有什么關(guān)系的目的,例如:

  • Identify a (single) object (not to be deleted by this function)

    表示一個(gè)(單一)對(duì)象(不會(huì)被本函數(shù)刪除)

  • Point to an object allocated on the free store (and delete it later)

    指向一個(gè)從自由存儲(chǔ)上獲取的對(duì)象(以后會(huì)刪除)

  • Hold the nullptr

    持有nullptr

  • Identify a C-style string (zero-terminated array of characters)

    表示一個(gè)C風(fēng)格字符串(以0結(jié)尾的字符數(shù)組)

  • Identify an array with a length specified separately

    表示一個(gè)數(shù)組,長度被另外定義

  • Identify a location in an array

    表示數(shù)組的首地址

This makes it hard to understand what the code does and is supposed to do. It complicates checking and tool support.

這樣做的結(jié)果是很難理解代碼在做什么和被預(yù)期做什么。無論是檢查還是工具支持都會(huì)復(fù)雜。

Example(示例)

void use(int* p, int n, char* s, int* q){    p[n - 1] = 666; // Bad: we don't know if p points to n elements;                    // assume it does not or use span<int>    cout << s;      // Bad: we don't know if that s points to a zero-terminated array of char;                    // assume it does not or use zstring    delete q;       // Bad: we don't know if *q is allocated on the free store;                    // assume it does not or use owner}

譯者注:這可能是我們每天都能見到的指針用法,很難看出它們到底是前面提供的那種用法。

better(稍好)

void use2(span<int> p, zstring s, owner<int*> q){    p[p.size() - 1] = 666; // OK, a range error can be caught    cout << s; // OK    delete q;  // OK}

Note(注意)

owner<T*> represents ownership, zstring represents a C-style string.

owner<T*>表現(xiàn)所有權(quán),zstring表達(dá)C風(fēng)格的字符串

Enforcement(實(shí)施建議)

  • (Simple) ((Bounds)) Warn for any arithmetic operation on an expression of pointer type that results in a value of pointer type.

    (簡單)((邊界))警告所有針對(duì)指針表達(dá)式,并得到指針類型結(jié)果的算數(shù)操作。

到此,關(guān)于“C++怎么使用T*或onwer<T*>指明唯一對(duì)象”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI