2020年7月11日 星期六

讀書心得 - C++ Primer (5th Edition) - Chapter 2 (3) - Type Aliases



Type Aliases

    創造資料型態的同義詞,通常是用 typedef 來去實現。
    typedef double wages;   // wages is a synonym for double
    typedef wages base, *p; // base is a synonym for double, p for double*

auto

    寫程式常會有想要一個 variable 去儲存 expression 的運算結果,但要事先知道 expression 的最終資料型態非常困難。所以有 auto 這個特別的資料型態,讓編譯器決定其資料型態,但相對的 variable 一定要 initialize。
    auto item = val1 + val2; // item initialized to the result of val1 + val2
    因為 auto 一次只能當一個資料型態,所以在宣告時如果是用 ", " 分開的複數宣告,資料型態要一致。
    auto i = 0, *p = &i;      // ok: i is int and p is a pointer to int
    auto sz = 0, pi = 3.14;   // error: inconsistent types for sz and pi
    auto 與 const 的互動,基本上 auto 會忽視 top level const ( 物件本身為 const )。
    const int ci = i, &cr = ci;
    auto b = ci;       // b is an int (top-level const in ci is dropped)
    auto c = cr;       // c is an int (cr is an alias for ci whose const is top-level)
    auto d = &i;       // d is an int*(& of an int object is int*)
    auto e = &ci;      // e is const int*(& of a const object is low-level const)
    若想要個 top level auto const,你只能在 auto 前加 const 修飾詞。
    const auto f = ci; // deduced type of ci is int; f has type const int

decltype()

    不同於 auto,decltype() 可以不用去計算 expression,只讓編譯器去分析其資料型態。下面程式碼,編譯器不會呼叫 f(),而是去檢查 f() 的 return 資料型態去給予 sum。
    decltype(f()) sum = x;     // sum has whatever type f returns
    decltype() 也可以與 top level const 互動。
    const int ci = 0, &cj = ci;
    decltype(ci) x = 0;     // x has type const int
    decltype(cj) y = x;     // y has type const int& and is bound to x
    decltype(cj) z;         // error: z is a reference and must be initialized
上一篇 :

Related Posts:

  • C/C++ - Constconst 基本用途     告知編譯器這個值為「常數」無法被修改。 一般作用 const int index = 5; index = 8; // error: assignment of read-only variable 'index' 作用於 * // const pointer char * const constptr = initptr; *constptr = 1… Read More
  • 讀書心得 - C++ Primer (5th Edition) - Chapter 4 (1) - Expression & Operator Expression 運算式 (expression) 由運算元 (operand) 與運算子 (operator) 所組成,最終計算產生一個結果 (result)。 Order of Evaluation 想要計算 expression 的話,就必須知道運算子運作及其先後順序。這裡的 * 號就不是 pointer 而是相乘,然後再來是基本的先乘除後加減。 cout << 5 + 10… Read More
  • 讀書心得 - C++ Primer (5th Edition) - Chapter 3 (3) - Array Array Array 為資料結構且跟 Vector 很像,都是裝同一 Type object 的 container。但 Array 為固定大小,所以不能增加 element,但也因為如此有時能提供較好的 run-time performance。 Initialize 在 C++ 裡 Array 的 Size 要為 constant expression,也就是 expression 能在 compile… Read More
  • 讀書心得 - C++ Primer (5th Edition) - Chapter 3 (1) - Namespace Namespace - using declaration 為了讀取 stdin,程式碼會是 std::cin。 :: 的左邊是告訴 compiler 去哪個 scope 找右邊的 operand。這有時會造成程式碼過於冗長,所以有了 using declaration 讓你更簡單的呼叫 namespace 底下的成員。 #include <iostream> using std::cin; … Read More
  • 讀書心得 - C++ Primer (5th Edition) - Chapter 3 (2) - Vector Library - vector Intro vector 是物件的集合,集合裡的物件都是同一 type,且每個物件都有 index 可以用找到該物件。vector 也是 container 和 class template 的一種。 vector<int> ivec; // ivec holds objects of type int vector&l… Read More

0 意見:

張貼留言

Popular Posts