Effective C++:零、Introduction
Terminology
每個函式的宣告,就可以看出它的 signature,也就是所有的參數型別和返回型別,例如:
numDigits 函式的簽名就是 std::size_t (int)
不過 C++ 官方對 signature 的定義不包括函式的返回型別。
definition 的任務是提供編譯器一些宣告式所遺漏的細節,對物件而言,definition 是編譯器為此物件撥發記憶體的地點,對 function 或 function template 而言,definition 提供了程式碼本體,對 class 或 class template 而言,definition 列出它們的成員。
Initialization 是「給予物件初值」的過程,對自定型別的物件而信,初始化由建構式執行。
所謂 default constructor ,是一個可被呼叫而不帶任何引數,又或是每個引數都有預設值的 constructor。
被宣告為 explicit 的建構式,會比 non-explicit 更受歡迎,因為它們禁止編譯器執行非預期(往往也不被期望)的型別轉換。
Copy constructor 是被用來「以同型物件來初始化自我物件」,Copy assignment operator 則是被用來「從另一個同型物件中拷貝其值到自我物件」。
如果果一個新物件被定義(如上例 w3),則一定會有個建構式被呼叫。
copy constructor 是一個重要的函式,因為它定義一個物件如何 passed by value。
其中 w 是以 by value 的方式傳給 hasAcceptableQuality,所以在上述呼叫中 aWidget 被複製到 w 體內,這個複製動作就是由 copy constructor 完成的。
不過以 Pass-by-value 傳遞用戶自定型別通常是個壞主意,Pass-by-referenct-to-const 往往是個比較好的選擇,詳見條款 20。
STL 是所謂的標準樣板函式庫(Standard Template Library),是 C++ 標準函式庫的一部分,致力於容器(ex: vector, list, set, map, ...)、迭代器(ex: vector::iterator, set::iterator, ...)、演算法(ex: for_each, find, sort, ...)及相關機能,許多相關機能以函式物件 (function objects) 實現,那是「行為像函式」的物件,這樣的物件來自於重載 operator() 的 classes。
Naming Conventions
const Rational operator*(const Rational& lhs, const Rational& rhs);
lhs means left hand side;
rhs means right hand side;
Terminology
每個函式的宣告,就可以看出它的 signature,也就是所有的參數型別和返回型別,例如:
numDigits 函式的簽名就是 std::size_t (int)
不過 C++ 官方對 signature 的定義不包括函式的返回型別。
definition 的任務是提供編譯器一些宣告式所遺漏的細節,對物件而言,definition 是編譯器為此物件撥發記憶體的地點,對 function 或 function template 而言,definition 提供了程式碼本體,對 class 或 class template 而言,definition 列出它們的成員。
int x; // 物件的定義式
std::size_t numDigits(int number) { // 函式的定義式
std::size_t digitsSoFar = 1;
while ((number / 10) != 0)
++digitsSoFar;
return digitsSoFar;
}
class Widget { // class 的定義式
public:
Widget();
~Widget();
...
};
template <typename T> // template 的定義式
class GraphNode {
public:
GraphNode();
~GraphNode();
...
};
Initialization 是「給予物件初值」的過程,對自定型別的物件而信,初始化由建構式執行。
所謂 default constructor ,是一個可被呼叫而不帶任何引數,又或是每個引數都有預設值的 constructor。
class A {
public:
A(); // default constructor
};
class B {
public:
explicit B(int x=0, bool b=true);
// default constructor, see below for info on "explicit"
};
class C {
public:
explicit C(int x); // not a default constructor
};
void doSomething(B bObject);
// a function taking an object of type B
B bObj1; // an object of type B
doSomething(bObj1); // fine, passes a B to doSomething
B bObj2(28); // fine, creates a B from the int 28
doSomething(28);
// error! doSomething takes a B, not an int,
// and there is no implicit conversion from int to B
doSomething(B(28));
// fine, uses the B constructor to explicitly convert
// the int to a B for this call.
被宣告為 explicit 的建構式,會比 non-explicit 更受歡迎,因為它們禁止編譯器執行非預期(往往也不被期望)的型別轉換。
Copy constructor 是被用來「以同型物件來初始化自我物件」,Copy assignment operator 則是被用來「從另一個同型物件中拷貝其值到自我物件」。
class Widget {
public:
Widget(); // default constructor
Widget(const Widget& rhs); // copy constructor
Widget& operator=(const Widget& rhs);
// copy assignment operator
...
};
Widget w1; // invoke default
Widget w2(w1); // invoke copy constructor
w1 = w2; // invoke copy assignment operator
Widget w3 = w2; // invoke copy constructor
如果果一個新物件被定義(如上例 w3),則一定會有個建構式被呼叫。
copy constructor 是一個重要的函式,因為它定義一個物件如何 passed by value。
bool hasAcceptableQuality(Widget w);
Widget aWidget;
if (hasAcceptableQuality(aWidget))
...
其中 w 是以 by value 的方式傳給 hasAcceptableQuality,所以在上述呼叫中 aWidget 被複製到 w 體內,這個複製動作就是由 copy constructor 完成的。
不過以 Pass-by-value 傳遞用戶自定型別通常是個壞主意,Pass-by-referenct-to-const 往往是個比較好的選擇,詳見條款 20。
STL 是所謂的標準樣板函式庫(Standard Template Library),是 C++ 標準函式庫的一部分,致力於容器(ex: vector, list, set, map, ...)、迭代器(ex: vector
Naming Conventions
const Rational operator*(const Rational& lhs, const Rational& rhs);
lhs means left hand side;
rhs means right hand side;
全站熱搜