四、const限定符【參考/指標/頂層/常量運算式】
const物件值不變,必須初始化,能完成此type的大部分operation,
一般,多檔案獨立變數,編譯初始化僅檔案內有效;
除非,(條件:初值不是常量運算式 and 宣告+定義 都有 extern);
//file.h extern const int bufSize; //file.cc extern const int bufSize=fcn();
//參考reference to const
不可通過此reference改變object的值,
ps:這并不代表,不可以通過其他reference來改變object的值,
“常量參考”:參考bind物件,本身就不可能再去bind其他物件了,so參考必是const的;reference本身不是object,變不了,
const type & 可以系結任何運算式,甚至可以打破型別限制——但非法;
double dVal=3.14; const int &ri=dVal; //原理 const int temp=dVal; const int &ri=temp; //temp,即 臨時量 temporary
//指標
pointer to const 指向常量的指標:存const type的object的address只能用它,
ps:這并不代表,它只能存const type的object的address;即不變的是object,pointer隨意,
so:reference/pointer to const 只是它們自以為自己 to const 了,
const pointer 常量指標:不變的是pointer,即不可以通過pointer來改變object的值,
ps:不變的是初始化了的address,object隨意,
double dVal=3.14; const double cd=3.141592; const double *pcd=&dVal; const double *const pcdc=&cd;
從右向左,pcdc是const pointer point to const double,
//頂層const
top-level const:必是object,任意type適用,
low-level const:參考和pointer to const,
const int ci=42; const int *pci=&ci;//low const int &r=ci;//low const int *const pci_=pci;//第一個const是low,第二個const是top
ps:low-level const 拷貝限制:二者都要底層const資格且可型別轉換,【難搞哦】
//常量運算式const expression
編譯時得值,且值不變,
角度刁鉆,不方便使用,提供constexpr宣告陳述句(可定義變數和函式)->供編譯器提前驗證其值是否是const expression,
constexpr的定義范圍:算數型別、參考、指標(初值nullptr/0/pointer to const,僅對pointer有效即頂層);一般在函式體外,函式體內可定義有效范圍超出函式體的變數【比較魔幻】
const int *p=nullptr; //p是 pointer to const int constexpr int *q=nullptr;//q是 const pointer to int
ps:constexpr會把其object置為頂層const,
五、處理型別【型別別名/auto/decltype】
//型別別名type alias
typedef double wages; typedef wages base,*p;
復合型別/常量:typedef定義的是型別別名,此宣告符包含型別修飾符,
typedef char *pstring; const pstring *ps; const pstring cstr=0; const char *cstr=0;//和樓上不是一個意思
C++11,別名宣告alias declaration:
using SI=Sale_item;
//auto->讓編譯器替你分析(through 初始值)運算式所屬型別,
auto定義的變數必須有初始值,且同一條宣告中所有變數base type必須相同,型別修飾符不算,
auto i=0,*p=&i; auto sz=0,pi=3.14;//錯
auto會忽略頂層const,保留底層const,
int i=0; const int ci=i,&cr=ci; auto d=&i; //d是指向int型pointer auto e=&ci; //e是指向const int型pointer const auto f=ci; //f是const int auto &h=42; //error:非常量參考不能bind字面值 const auto &j=42; //常量參考可以bind字面值 auto k=ci,&l=i; //k是int,l是int型參考 auto &m=ci,*p=&ci; //m是const int型ci的參考,p是指向const int型 ci 的指標 auto &n=i,*p2=&ci; //error:n是int型i的參考,p2是指向const int型i的指標
設定一個型別為auto的參考時,初始值中的頂層常量屬性任然保留,和往常一樣,如果我們給初始值系結一個參考,則此時的常量就不是頂層常量了,【不懂】
//decltype->要運算式的回傳型別,但不要值,使目標初始化,
編譯器分析運算式并得到它的型別,但不算值,
若,運算式是變數,回傳變數type(包括頂層const和參考):
const int ci=0,&cj=ci; decltype(ci) x=0; decltype(cj) y=x;//此處cj就是cj,而不是ci,即y系結x
若,運算式不是變數,回傳結果對應型別:
int i=42,*p=&i,&r=i; decltype(r+0) b;//b是int型 decltype(*p) c;//error:c是int&,必須初始化
若,多加一對(),即decltype((xxx)),編譯器會誤以為其是運算式,而回傳參考型別(因為賦值陳述句左值是變數):
decltype((i)) d;//error:d是int&,必須初始化
六、自定義資料結構【.../頭檔案/前處理器】
struct/class什么什么的,
ps:可為類內的data member提供一個類內初始值in-class initializer,
//頭檔案
包含那些只能被定義一次的物體,
隱式多次包含->預處理,保證程式安全正常作業,
//前處理器preprocessor->頭檔案保護符header guard
#include,編譯前執行,改變部分程式
預處理變數:已定義/未定義,必須唯一,一般大寫
#define:設定預處理變數;
#ifdef/#ifndef:變數已定義,為真/變數未定義,為真;
#endif:為止;
#ifndef SALES_DATA_H #define SALES_DATA_H #include <string> struct Sales_data{ std::string bookNo; unsigned units_sold; double revenue=0.0; }; #endif
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59723.html
標籤:C++
上一篇:const與指標、參考
