Some other issues in C tsaiwncsie nctu edu
- Slides: 49
Some other issues in C++ 蔡文能 tsaiwn@csie. nctu. edu. tw tsaiwn@cs. nctu. edu. tw 交通大學資訊 程學系 2006/08/23 1 交通大學資訓 程學系 蔡文能
http: //tsaiwn. net/cpp/ Agenda http: //www. csie. nctu. edu. tw/~tsaiwn/cpp • • Namespace static ? const ? cast in C++ Class Library STL – Standard Template Library Exception handling 2 交通大學資訓 程學系 蔡文能
Namespaces • variables with same name and different scopes can overlap – need to distinguish them • a namespace defines a scope for local and global identifiers. – body delimited by braces {} – use (: : ) to access namespace members: 例如 std: : cout namespace_name: : member – or, a using statement must occur before name is used using namespace_name; -members of the namespace do not need a prefix – not guaranteed to be unique – can be nested using namespace std; //for STL 交通大學資訓 程學系 蔡文能 3
神奇的 “static” ü On a global variable or a function static long my. Data[38]; //information hiding static void my. Function(float); – Tells the linker not to export the variable or function. – Makes the identifier “file scope, ” as the linker will not use it fulfill dependencies from other files. ü On a local variable in a function void some. Func(void) { static int array[4000]; } Places the variable off the stack. This has the side-effect that it retains it value across calls. It is often used when a variable is too large to be put on the stack. (auto變數則在 stack) ü On a class member data or member function (next slide) 4 交通大學資訓 程學系 蔡文能
Static Global 變數 #include <stdio. h> 參考K&R課本 4. 6節 #define BUFSIZE 100 static char buf[BUFSIZE]; int bufp = 0; 別的檔案中任何function都 int getch( ) { 看不見這個 static 的變數 /*. . . */ } void ungetch(int c) { /*. . . */ 也參考stack的push和pop寫在同一獨 } 立 file 中, push和pop共享 data 交通大學資訓 程學系 蔡文能 5
再談Static Global 變數 #include <stdio. h> 參考K&R課本 4. 6節 #define RAND_MAX 65535 static unsigned long seed=0; int rand( ) { seed = seed * 1103515245 + 12345; return seed % (RAND_MAX+1); } void srand(int newseed) { seed = newseed; } 交通大學資訓 程學系 蔡文能 Pseudo random number 6
Static Local變數 (1/2) #include <iostream. h> int fa( ) { int x = 1; return x++; /*先取其值, 再做 ++ */ } int fb( ) { static int x = 1; /* 注意 static int x = 1; */ return x++; } int main( ) { cout << "fa( )=" << fa( )<<fa( ) << endl; cout << "fb( )=" << fb( )<<fb( ) << endl; return 0; /* 0 in main( ) means OK */ } return x++; 和 return ++x; 不同 ! 7 交通大學資訓 程學系 蔡文能
Static class member • static member不需要透過任何的object存取,在無任何 object時可透過member selection operators (就是 : : 或Java 仍用. )來存取. • Static member functions – Also known as class function (class methods) – Can be called without any instance, for example: m = Mankind: : howmany( ); – Can NOT access instance variables • Static member data – Also known as class variable – All objects of a class share one copy of a static data member (non-static member data = instance variable) – Usage example: x = Mankind: : data_which_is_static; – They are not global variables; they have class scope All member functions are shared between objects, even the non-static member functions 交通大學資訓 程學系 蔡文能 10
神奇的 “const” ü Const object is NOT modifiable ü const x = 3. 14159; // 在 C 與較舊C++ compile會過; 問題在哪裡? ü Const parameter can NOT be modified in the function Return. Type Function. Name(long x, const Student y){ /*… to modify y is NOT allowed! 即 y 不可當左值 */ }; ü Const member function can NOT modify the object Return. Type Function. Name(param 1, param 2…) const; Return. Type Function. Name(param 1, param 2…) const { /*… */}; ü Constructors / Destructors cannot be const - They need to initialize variables (therefore modifying them) const 是不准變的變數! 也佔一塊記憶體! 那 #define nsize 99 呢? 交通大學資訓 程學系 蔡文能 11
Review: Constants 常數 • #define MAX_LOOP 500 – 此稱 define macro 巨集 – 是C的前處理器之句子, 結尾不必有分號 – 之後用到 MAX_LOOP 會被換成 500 • const double delta = 0. 0001; ^^^^^^^^ 不可變的變數叫const • const PI = 3. 14159; /* Error result? PI為 3*/ 12 交通大學資訓 程學系 蔡文能
Character Literal • 單引號夾住是 char – 'A' 相當於 65 (assume using ASCII) – 'a' 相當於 97 (assume using ASCII) – '0' 相當於 48 (assume using ASCII) • Wide character ? • Escape Sequence 逃脫序列串 – 'n' – '101' – 'x 41' == '