本篇內(nèi)容主要講解“C++怎么避免基于全局狀態(tài)的錯誤處理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++怎么避免基于全局狀態(tài)的錯誤處理”吧!
成都創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、高青網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5高端網(wǎng)站建設、商城系統(tǒng)網(wǎng)站開發(fā)、集團公司官網(wǎng)建設、外貿(mào)營銷網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為高青等各大城市提供網(wǎng)站開發(fā)制作服務。
E.28:避免基于全局狀態(tài)的錯誤處理(例如errno)
Global state is hard to manage and it is easy to forget to check it. When did you last test the return value of printf()?
全局狀態(tài)難于管理而且容易忘記檢查。你上一次檢查printf的返回值是什么是時候?
Example, bad(反面示例)
int last_err;
void f(int n)
{
// ...
p = static_cast(malloc(n * sizeof(X)));
if (!p) last_err = -1; // error if memory is exhausted
// ...
}
C-style error handling is based on the global variable errno, so it is essentially impossible to avoid this style completely.
C風格錯誤處理基于全局變量errno,因此這種風格的處理方式在本質(zhì)上無法避免。
到此,相信大家對“C++怎么避免基于全局狀態(tài)的錯誤處理”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!