Share
Embed
Cat System Workshop #19 - C++ Course Lession_1
Lesson 1 - C++ Interfaces & Conventions by Luba Tang
https://skymizer.com/
Skymizer
September 05, 2017
Transcript
44 slides
-
C++ Interfaces and Conventions n 今天不教的東⻄ – 怎麼寫出合法的 C++ n
今天要教的東⻄ – 怎麼寫出社群能接受的 C++ -
Permissions class Ha : public A, protected B, private C
{ public: void T(); virtual void X(); protected: void U(); virtual void Y(); private: void V(); virtual void Z() ; }; Public 大家都知道 Protected 只有自己的子孫知道 Private 只有我知道 Virtual 可以改寫 -
Virtual class Ha : public A, protected B, private C
{ public: virtual void X(); protected: virtual void Y() { } private: virtual void Z() = 0; }; Virtual 可以改寫 Trivial Virtual 其中會有一個人改寫 Pure Virtual 一定要改寫 -
孫類別必改寫 - 目前沒有程式語言能確保此行為 • 飛行器期待所有的子孫類別都要改寫 fly() • 一但飛機已經改寫了 fly(),直升機忘記寫 fly()
編譯器不會報錯 • C++ 語⾔缺乏描述「孫類別必定要改寫」的語法 飛行器 飛機 直升機 -
社群慣例 - 編譯器不會保護你 class He { public: void action() {
doAction(); } protected: void action() { doAction(); } private: virtual void doAction() = 0; }; 孫類別必改寫 Private -
你想幹什麼?到底是要不要改寫? class He { public: virtual void Y() { Private();
} protected: virtual void V() { Private(); } private: virtual void Private() = 0; }; 孫類別必改寫 Private 可選擇改寫子類別 (誤) -
Reference int a int& r = a; // reference就是alias的意思 •
r和a有相同的記憶體空間,r是a的別名 • 由於是別名,因此,本體必存在,保證存在性 • 因為保證存在性,因此reference後必須有本體 • Pointer int* a不保證存在性,可能是NULL -
parameter • f( int a ) – copy保證存在性 – 不需判斷NULL
– 不修改參數 – 有複製成本,不適用於大物件 • f( int* a ) – pointer 不保證存在性 if( a==NULL) – 需判斷NULL – 必修改參數 – 無複製成本,適用於大物件 • f( int& a ) – reference保證存在性 – 不需判斷NULL – 必修改參數 – 無複製成本,適用於大物件 -
const parameter • f( const int a ) – 你腦袋壞掉
• f( const int* a ) = f( int* const a ) – pointer 不保證存在性 if( a==NULL) – 需判斷NULL – 不修改參數 – 無複製成本,適用於大物件 • f( const int& a ) – reference保證存在性 – 不需判斷NULL – 不修改參數 – 無複製成本,適用於大物件 – 是更好的 f( int a ) -
Return Value • int f() – 就一份copy可以隨便搞 • int* f()
– 可能是NULL – 為內部member variable,或者是factory • int& f() – 保證存在性,但是必為內部member variable – 通透性寫法,基本上要盡量避免 -
Constant Return Value • const int f() – 找你夥伴的麻煩,還是有機會,但是罕見 •
const int* f() • 找你夥伴的麻煩 • const int& f() – 找你夥伴的麻煩 -
Constant Member Function class A { • const int f()
const – 找你夥伴的麻煩,而且你腦袋壞掉了 • const int* f() const – 可能是NULL,必為內部member variable – 不是factory • const int& f() const – 必存在 – 必為內部member variable – 不是factory } -
Note • 別忘了 reference 會擴大 scope • Constant member function
可以修改 global variable • Constant member function 只能使用 constant member function -
藉由 A,B 獲得永生 class A { • A(B); }; •
A 需要有 B 才能生成 • 隱含著一旦 A 存在,則系統就存在一個 B -
B 與 A 生死與共 class A { • A(const B&);
• A(B&); }; • 若失去了 B,則 A 沒有意義 • A 的生命週期,必在 B 之內 -
少用 class A { • A(B* pParent); • A(const B*
pParent); }; • 通常是歷史遺跡或者為了和 C 整合,如 Qt • pParent == null 對結果有影響 -
B 對 A 不是必要的 class A { • A(); •
setB(B); • setB(const B&); • setB(B*); }; • 優先考慮,從 aggregation 轉變成 use,減少 dependency -
繼承 A 的人,一定要懂的性質 class A { • A(B pB =
SOME_CONSTANT); • A(B* pB = SOME_POINTER); }; • B 對使用者沒意義 • B 對開發者意義非凡,是 A 極重要的 feature -
C++ Template • C++ Template – Functional Language in C++
• Language structure – Identifier • Constant • Variable – Condition – Loop • From the ground up – Fibonacci example -
Template 的目的 • 從繼承體系為基礎改為界面為基礎 // array of integers class Int
: public Type {}; class Array { Type& operator[](uint i); }; class IntArray : public Array { Int& operator[](uint i); }; Type Int Array IntArray Co-variant return type 必須繼承 Type -
Template 的目的 • 從繼承體系為基礎改為界面為基礎 • 編譯器保護您 // array of integers
class Int { static void doSomething(); }; template<typename T> class Array { T& operator[](uint i) { T::doSomething(); } }; typedef Array<Int> IntArray; Array IntArray T Int Int Constraint relation Int 不用繼承任何人, 但是要保證存在被使 用的 interfaces -
Example 1 – STL vector class Int { public: Int(int&
pValue) : m_Value(pValue) {} private: int& m_Value; }; typedef vector<Int> IntArray void foo() { IntArray array; } -
Interfaces used by STL containers • STL Containers (vector, list,
map, etc.) 所使用到的 interfaces – default constructor – destructor class Int { public: Int(int& pValue) : m_Value(pValue) {} private: int& m_Value; }; typedef vector<Int> IntArray void foo() { IntArray array; } 因為有 reference,compiler 無 法自動幫 Int 建立 default constructor 與 destructor -
Example 2 – STL vector class Int { public: Int(int&
pValue) : m_Value(pValue) {} private: int& m_Value; }; typedef vector<Int> IntArray; -
Only used interfaces are instantiation • 只有被用到的 template class 才會
instantiation – 雖然有被定義,但是卻不存在任何 function 使用到,則 compiler 不會 instantiate 該 template class – 很容易出錯而不自知,一定要搭配 unit test class Int { public: Int(int& pValue) : m_Value(pValue) {} private: int& m_Value; }; typedef vector<Int> IntArray 只有被定義,沒有被使用 不會 instantiation -
可以用在 class, member, 與 function 上 template<class T> class Object
{}; class Foo { public: template<class T> void foo(int); }; template<class T> void foo(int); -
可以透過 template 使用 sub-class class Foo { public: class Iterator
{ }; }; template <typename T> class foo { public: typedef T::Iterator iterator; } 使用者有義務告知編譯器 identifier 的型別 要加 typename sub class 表示 class Iterator 在型別上相依於 class Foo typedef typename T::Iterator iterator; -
Agenda • C++ Template – Functional Language in C++ •
Language structure – Identifier • Constant • Variable – Condition – Loop • From the ground up – Fibonacci example -
Identifier - Template Arguments • 學語言,首先先學 identifier 5 int a;
void foo(); • C++ template meta programming 當中,identifier 即為 template arguments • Template arguments 有三種 – 常數:Non-type arguments – 變數: • Type arguments • Template template argument -
常數:Non-type arguments • 可以在 compile-time 求出數值的東西可以當作 template argument – Integral
arguments – Constant expression – Address argument template<size_t N> class Foo {}; Foo<5> foo1; Foo<10> foo2; 5 與 10 為常數 -
變數:Type arguments • None-template type 都是變數。 • 最常見的 template argument
template<typename N> class Foo {}; Foo<int> foo1; Foo<A> foo2; int 與 A 為變數 -
對變數設值/取值 • 設值:在 class 內給定常數數值,如 enum 或 constant member variables
template<size_t N> class Foo {}; Foo<Variable::Value> foo; // get the value class Variable { public: enum { Value = 10; // assign a value }; }; ‣ 取值:在 template argument 拿出 enum 或 constant member variable 的值。 -
判斷式 – template specification • Template specification 就是一系列的 if/else •
原 template 為 else part • Condition 是由上而下,先碰到的先 match template<size_t N> class Foo { enum { Value = 6}; }; template<> class Foo<5> { enum { Value = 5}; }; template<> class Foo<10> { enum { Value = 10}; }; if (N == 5) { Value = 5; } else if (N == 10) { Value = 10; } else { Value = 6; } -
變數判斷式 – template partial specification • Partial specification 表示只有部分 template
argument 已知 template<typename A, typename B> class Foo { enum { Value = 6}; }; template<typename A> class Foo<A, int> { enum { Value = 5}; }; template<typename B> class Foo<double, B> { enum { Value = 10}; }; if (B == int) { Value = 5; } else if (A == double) { Value = 10; } else { Value = 6; } -
迴圈 – 自我使用的 constant expression • Forever loop template<size_t N>
class Foo { enum { Value = Foo<N -1>::Value }; }; -
迴圈 – 自我使用的 constant expression for (size_t i = 0;
i < 10; ++i) Value = i; template<size_t N> class Foo { enum { Value = Foo<N -1>::Value }; }; template<> class Foo<0> { enum { Value = 0 }; }; int a = Foo<10>::Value; // a == 10 自我使用的 constant expression,即為迴圈 Template specification 即為迴 圈終止條件 執行一個迴圈 -
Agenda • C++ Template – Functional Language in C++ •
Language structure – Identifier • Constant • Variable – Condition – Loop • From the ground up – Fibonacci example -
Example: Fibnocci // Run-time version: 0 1 2 3 5
8 13 … int fibnocci(int index) { if (0 == index) return 0; if (1 == index) return 1; return fibnocci(index – 1) + fibnocci(index – 2); } -
Example: Fibnocci template<size_t N> struct fib { enum { value
= fib<N-1>::value + fib<N-2>::value }; }; template<> struct fib<0> { enum { value = 0 }; }; template<> struct fib<1> { enum { value = 1 }; }; int a = fib<5>::value; // a == 8 -
C++ Meta Programming 使用時機 1. 當所有事情都是以 Type 在運行時 2. 當所有事情其實都可以從
Variable 改為 Type 3. Performance Critical Code 4. 不在乎編譯時間 -
C++ Interfaces and Conventions n 今天不教的東⻄ – 怎麼寫出合法的 C++ n
今天要教的東⻄ – 怎麼寫出社群能接受的 C++ 1. Permission 2. Virtual 3. Reference/Pointer 4. Parameter 5. Return Type 6. Constructor Design 7. Template Design 8. C++ Template Meta Programming