Chapter 2 Note of C++ Primer By Alex

Primitive Built-in Types

Variables

Compound Types

const Qualifier

Dealing with Types

As programs get more complicated, using types also gets more complicated.
Two different ways:

  • Hard to "spell"
    • Forms are tedious and error-prone to write
    • Forms obscure the types' purpose or meaning
  • Hard to determine the exact type we need

Type Aliases

Traditionally, we use a typedef:

typedef double wages;
typedef wages base, \*p;

The keyword typedef may appear as a part of the base type of a declaration. Declarations that include 'typedef' define type aliases rather than variables. The declarators can include type modifiers, too.
The C++ 11 way, alias declaration:
using SI = Sales_item

Pointers, const, and Type Aliases

  typedef char \*pstring;
  const pstring cstr = 0; // cstr is a constant pnt to char
  const pstring \*ps; // ps is a pnt to a constant pnt to char

It can be tempting, albeit incorrect, to intepret a declaration that uses a type alias by conceptually replacing the alias with its corresponding type:
const char *cstr = 0; //wrong intepretation of const pstring cstr

The auto Type Specifier

Deduce the type of vars from the initializers. A var that uses auto as its type specifier must have an initializer.
A declaration can involve only a single base type. So the initializers for all vars in the declaration must have types that are consistent with each other.

Compound Types, const, and auto

The compiler adjusts the type to conform to normal initialization rules.

First, when using a ref as an initializer, the initializer is the corresponding object.

int i = 0, &r = i;
auto a = r; //a is an int

Second, auto ordinarily ignores top-level consts.

const int ci = i, &cr = ci;
auto b = ci; // int
auto c = cr; // int
auto d = &i; // int*
auto e = &ci; // const int*

If we want deduced type to have a top-level const, we must say so explicitly:
const auto f = ci; //deduced type of ci is int, f has a type const int
We can also specify that we want a ref to the auto-deduced type:

auto &g       = ci; // g is a const int& that is bound to ci
auto &h       = 42; // error: we can't bind a plain ref to a literal
const auto &j = 42; // ok: we can bind a const ref to a literal

When we ask for a ref to an auto-deduced type, top-level constsin the initializer are not ignored.
In a single statement, the initializers must provide consistent auto-deduced types:

auto k  = ci, &l = i; // k is int, l is int&
auto &m = ci, *p = &ci; // m is const int&, p is a pnt to const int
// error: type deduced from i is int, while type deduced from &ci is const int
auto &n = i, *p2 = &ci;

The decltype Type Specifier

Sometimes, we want to define a var with a type that the compiler deduces from an expr but don't want to use the expr to init the var.
C++ 11 feature, decltype returns the type of its operand.
decltype(f()) sum = x; // sum has the type f returns
The way decltype handles top-level const and refs differs subtly from the way auto does.
Whe the expr to which we apply decltype is a var, decltype returna the type of that var, including top-level const and refs:

const int ci = 0, &cj = ci;
decltype(ci) x = 0; // x has type const int
decltype(cj) y = x; // y is a const int& cound to x
decltype(cj) z; // error: z is a ref and must be initialized

decltype is the only context in which a var defined as a ref is not treated as a synonym for the obj to which it refers.

decltype and References

When applying decltype to an expr that isn't a var, we get the type that the expr yields. decltype returns a ref type for exprs that yield objs that can stand on the left-hand side of the assignment.
Assignment is an example of an expr that yields a ref type. The type is a ref to the type of the left-hand operand.

Defining Our Own Data Structure

Defining the Sales_data Type

struct Sales_data {
    std::string bookNo;
    unsigned units_sold;
    double revenue = 0.0;
};

Class Data Members

members, data members
C++ 11: in-class initializer
In-class initializers are restricted as to the forms we can use: They must either be enclosed inside curly braces or follow an = sign.

Using the Sales_data Class

Reading Data into a Sales_data Object

Writing Our Own Header Files

Headers usually contain entities that can be defined only once in any given file.

A Brief Introduction to the Preprocessor

The preprocessor is a program that runs before the compiler and changes the source text of our programs.
Preprocessor vars have one or two possible states: defined or not defined.
Preprocessor var names don't respect C++ scoping rules.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末噩凹,一起剝皮案震驚了整個(gè)濱河市辛慰,隨后出現(xiàn)的幾起案子幢竹,更是在濱河造成了極大的恐慌,老刑警劉巖采桃,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡沉噩,警方通過查閱死者的電腦和手機(jī)吏祸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門对蒲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人贡翘,你說我怎么就攤上這事蹈矮。” “怎么了鸣驱?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵泛鸟,是天一觀的道長。 經(jīng)常有香客問我丐巫,道長谈况,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任递胧,我火速辦了婚禮碑韵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘缎脾。我一直安慰自己祝闻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布遗菠。 她就那樣靜靜地躺著联喘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪辙纬。 梳的紋絲不亂的頭發(fā)上豁遭,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音贺拣,去河邊找鬼蓖谢。 笑死捂蕴,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的闪幽。 我是一名探鬼主播啥辨,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼盯腌!你這毒婦竟也來了溉知?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤腕够,失蹤者是張志新(化名)和其女友劉穎级乍,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體帚湘,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卡者,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了客们。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片崇决。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖底挫,靈堂內(nèi)的尸體忽然破棺而出恒傻,到底是詐尸還是另有隱情,我是刑警寧澤建邓,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布盈厘,位于F島的核電站,受9級(jí)特大地震影響官边,放射性物質(zhì)發(fā)生泄漏沸手。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一注簿、第九天 我趴在偏房一處隱蔽的房頂上張望契吉。 院中可真熱鬧,春花似錦诡渴、人聲如沸捐晶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惑灵。三九已至,卻和暖如春眼耀,著一層夾襖步出監(jiān)牢的瞬間英支,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國打工哮伟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留干花,地道東北人鸯屿。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像把敢,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谅辣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容