C++自修入門實境秀、C++ Primer 5版研讀秀 32/~第6章函式-頁205 自動物件(Automatic Objects)
頁196
Functions Are Exited during the Search for a Handler
搜尋處理器時會退出函式
應該是說當有錯誤(異常、例外)發生時,因為要找到對於此錯誤相應的處理程序,所以得退出原來函式的執行。即類似前面介紹過的jump statements也。所以說catch是具有條件式的跳躍式,一如switch……case……一般。(ps.catch不會離開、跳出,throw才會。然這裡謂跳躍式的是指在一群catch中,2:23:00和case一樣,只會選符合條件的來執行行,其他就跳開了。當然沒有break述句時,其後的case也會被執行;應該也是要符合case label值與條件式札等的才會執行吧) catch 是像case,沒有break;述句就不會跳開
Exit 其實就是 jump 在VBA中也是用Exit作關鍵字也
try就是try it 測試的意思
11:14
a library function named terminate .
14:00
Caution: Writing Exception Safe Code is Hard
Programs that properly “clean up” during exception handling are said to be exception safe. Writing exception safe code is surprisingly hard, and (largely) beyond the scope of this language Primer.
exception safe:安穩的例外處理式
safe 令人放心的,沒有疑慮的
或翻成「例外處理無虞」,用「無虞」來翻safe
即「不虞匱乏」的「不虞」
「不疑有他」的「不疑」
甚至可以用 安 ②單字想複詞 →安全 全→完全
用「完全」來翻,「完全或完美的例外處理」
就是不能照字典例翻成「安全」,因為與中文語境不合也。
30:00
不愛文言,就別用「者」字:
At the point where the exception occurs, some of the computations that the caller requested may have been done, while others remain undone.
在例外發生的地方,呼叫者所請求的某些 計算可能已經完成,然而其他的依然未完成。
這裡的「者」什麼意思啊?還是「的人」而已嗎?
44:15
頁197
5.6.3. Standard Exceptions
標準例外
程式庫所定義的例外類別 exception classes
存於四個標頭檔中: 57:50
exception header
stdexcept header
new header
type_info header
51:00
表5.1
54:43
overflow_error、underflow_error即「過與不及」「過」和「不及」。
1:2:48
這些例外類別的運算極其有限:
The library exception classes have only a few operations. We can create, copy, and assign objects of any of the exception types.
1:24:55 有3種例外型別不能設定初始式,只能用預設初始化:
We can only default initialize (§ 2.2.1, p. 43) exception, bad_alloc, and bad_cast objects; it is not possible to provide an initializer for objects of these exception types.
1:8:20
what() 成員函式,不帶引數,傳回一個指向常值字元的指標
returns a const char* that points to a C-style character string (§ 3.5.4, p. 122).
1:13:00
頁198
練習5.23
1:14:54
1:17:00
文言?白話?「除以」
Write a program that reads two integers from the standard input and prints the result of dividing the first number by the second.
寫一個程式從標準輸入讀取兩個整數,並印出第一個數字除以第二個的結果。
1:39:10 throw 大概是自訂義的錯誤(例外),所以其條件式是由撰者自行寫出,如頁194:
if (item1.isbn() != item2.isbn())
throw runtime_error("Data must refer to same ISBN");
而try則是由程式執行時它自己去抓的。所以如果程式無法自己抓到,就應該自己加上條件式來判斷,並擲出例外情形,所以下式執行時無法try catch到,就只能用if throw了!
int i1, i2;
int d=0;
cout << "論輸入2個數字來相除" << endl;
while (cin>>i1>>i2)
{
try
{
d= i1 / i2;
}
//catch (const std::exception& except)
catch (runtime_error except)
{
cout<<except.what()<<endl;
}
cout << i1 << " divided by " << i2 << " is " << d<<endl;
}
如果執行時不會擲出異常(即如果try block抓不到例外情形),就得自己寫throw來讓try抓到,像下面這樣catch才抓得到! 2:4:50
2:2:20
int i1, i2;
int d=0;
cout << "論輸入2個數字來相除" << endl;
while (cin>>i1>>i2)
{
if (i2 == 0)
{
try
{
throw runtime_error("不能除以0!");
}
catch (const std::exception& e)
{
cout<<e.what()<<endl;
break;
//return;//和break在這裡效用一樣
}
}
d= i1 / i2;
cout << i1 << " divided by " << i2 << " is " << d<<endl;
}
練習5.24
2:5:19
已見前題
練習5.25
int i1, i2;
int d=0;
cout << "論輸入2個數字來相除" << endl;
while (cin>>i1>>i2)
{
if (i2 == 0)
{
try
{
throw runtime_error("不能除以0!");
}
catch (const std::exception& e)
{
cout<<e.what()<<"\n重新輸入!"<<endl;
//break;
//return;//和break在這裡效用一樣
}
}
else
{
d= i1 / i2;
cout << i1 << " divided by " << i2 << " is " << d<<endl;
}
}
2:17:18
頁199
Chapter Summary
• continue , which stops the current iteration of a loop.
• break , which exits a loop or switch statement.
try and catch , which define a try block enclosing a sequence of statements that might throw an exception. The catch clause(s) are intended to handle the exception(s) that the enclosed code might throw.
• throw expression statements, which exit a block of code, transferring control to an associated catch clause.
所以throw還是要和catch搭配使用嘛,不是只有try才和catch配著用
• return , which stops execution of a function. (We’ll cover return statements in Chapter 6 .)
return主要是針對函式
2:30:00 運算式述句
In addition, there are expression statements and declaration statements.
An expression statement causes the subject expression to be evaluated.
一個運算式述句會使其中的運算式被估算。
宣告和定義(述句)
Declarations and definitions of variables were described in Chapter 2 .(頁44)
已定義的詞彙
就是已學會的觀念或已學過的
block Sequence of zero or more statements enclosed in curly braces.
可見block與大括號是分不開的
A block is a statement
不管它裡頭有多少「子句」(述句)
複合述句就是block的同義詞,block就是複合述句
compound statement Synonym for block.
dangling else Colloquial term used to refer to the problem of how to process nested if statements in which there are more if s than else s.
in which 翻成「在這種」
2:50:00 exception classes
Set of classes defined by the standard library
一組由標準程式庫定義的類別。
頁200
2:53:50
exception handler和catch clause是同義詞
Code that deals with an exception raised in another part of the program. Synonym for catch clause.
所以例外處理都是在catch中執行的嘛
2:57:00
exception safe翻成「不怕例外的」,也可以:
Term used to describe programs that behave correctly when exceptions are thrown.
意思是在例外情形下也是很安全的;即使發生例外情形,也不必擔心(無虞)的意思
expression statement運算式述句
An expression followed by a semicolon. An expression statement causes the expression to be evaluated.
這裡就沒有subject了!前面說:
An expression statement causes the subject expression to be evaluated.
3:1:20
flow of control
Execution path through a program.
⑦先抓動詞 這句沒有動詞?
可見control在這裡就是 execution 或execution path
3:9:40
if else statement
Conditional execution of code following the if or the else , depending on the truth value of the condition.
這句也沒動詞?下面也是:
if statement
Conditional execution based on the value of the specified condition.
大概單一「句子」來定義詞彙是,就未必須用完整(含動詞)的句子。
3:16:50
raise和throw是同義詞:
raise Often used as a synonym for throw. C++ programmers speak of “throwing” or “raising” an exception interchangeably.
3:24:00
throw expression
Expression that interrupts the current execution path. Each throw throws an object and transfers control to the nearest enclosing catch clause that can handle the type of exception that is thrown.
愈見throw與catch密不可分,不亞於try
3:32:30
頁201
Chapter 6. Functions
函式(函數)
3:36:30
函式可以多載(重載(overload))
functions can be overloaded, which means that we can use the same name for several different functions.
多載的意思就是不同的內容可以有相同的名字
頁202
一個有名字的區塊,就叫函式:
A function is a block of code with a name.
6.1. Function Basics
3:41:20
可見在定義的時候是「parameters」而在調用或呼叫時是叫「arguments」
3:46:40
The arguments are used to initialize the function’s parameters.
這樣,argument和parameter的關係就很清楚了
3:48:00
Writing a Function
3:55:40
Calling a Function
頁203
4:6:20
Parameters and Arguments
參數和引數
引數就是作為函式參數的初始器:
Arguments are the initializers for a function’s parameters.
4:13:30
"hello"
這個"hello"型別是 const char*(即字元陣列(character arrays)),不是string!
字串字面值(string literal)均是如此
頁204
4:17:20
Function Parameter List 函式參數列
函式的參數序列(清單)
是不能被省略的
A function’s parameter list can be empty but cannot be omitted.
這裡所謂的「omit」是指連圓括號都去掉了。
For compatibility with C, we also can use the keyword void to indicate that there are no parameters:
為了和C語言相容,還可以用void來指定不帶參數
void f1() { /* ... */ } // implicit void parameter list
void f2(void){ /* ... */ } // explicit void parameter list
4:26:30
declarator
即相當於宣告中的識別項(identifier)或變數名稱
4:32:00 可以有不具名的參數,通常是不會被用到的參數;但調用函式時仍須為它傳遞引數:
Occasionally a function has a parameter that is not used. Such parameters are often left unnamed, to indicate that they aren’t used. Leaving a parameter unnamed doesn’t change the number of arguments that a call must supply. A call must supply an argument for every parameter, even if that parameter isn’t used.
4:34:55
Function Return Type
函式不能傳回陣列和函式型別(函式也是型別?)
However, the return type may not be an array type (§ 3.5 , p. 113 ) or a function type.
但是卻可以傳回一個指向陣列或函式的指標(pointer)
However, a function may return a pointer to an array or a function.
4:39:40
6.1.1. Local Objects
6.1.1區域物件
生命週期終於出現了:
In C++, names have scope (§ 2.2.4 , p. 48 ), and objects have lifetimes . It
原來「名稱」是「範疇」,而「物件」是「生命週期」。
而「範疇」是「visible」
• The scope of a name is the part of the program’s text in which that name is visible.
範疇就是在程式碼的某段文字中,那個名稱是可見(可用)的
4:46:00
local variables . 區域變數
They are “local” to that function and hide declarations of the same name made in an outer scope.
對於區域/範疇外同名的變數而言,區域變數就「覆寫」它了
頁205
練習6.1
引數為參數的初始器
練習6.2
4:50:40
// (a)
string f() {
string s;
// ...
return s;
}
傳回型別要和函式定義的一致
// (b)
void f2(int i) { /* ... */ }
一定要有傳回型別
// (c)
int calc(int v1, int v2) {/* ... */
}
參數名不能重複!
//(d)
double square(double x) { return x * x; }
函式定義一定要有大括號,作為body或block的範疇標示
4:58:59
練習6.3
int fact(int f){
int r=1;
for (size_t i = 1; i <= f; i++)
{
r *= i;
}
return r;
}
練習6.4
5:7:10
void fact() {
int r = 1,f;
cout<< "請輸入一個數字以求其階乘" << endl;
cin >> f;
for (size_t i = 1; i <= f; i++)
{
r *= i;
}
cout << r<< endl;
return ;
}
int main() {
fact();
return 0;
}
5:12:20
練習6.5
void absv(int ab) {
unsigned r = 1;
if (ab<0)
{
r = ab * -1;
}
else
{
r = ab;
}
cout << r<< endl;
return ;
}
5:17:30
Objects defined outside any function exist throughout the program’s execution.
定義在所有函式外的物件在程式的整個執行期間都會存在。
只要是定義在任何函式外面的物件或變數(具名物件),其生命週期是與整個應用程式相終始的。
destroyed 翻成被撤銷、被銷毀、被取消、被歸零
自動物件(Automatic Objects)
⑧找對主詞 什麼東東自動?
留言