标签:
(一)volatile
1
2
3
4
|
XBYTE[2]=0x55; XBYTE[2]=0x56; XBYTE[2]=0x57; XBYTE[2]=0x58; |
1
2
3
4
|
int square( volatile int *ptr) { return ((*ptr) * (*ptr)); } |
1
2
3
4
5
6
7
|
int square( volatile int * &ptr) //这里参数应该申明为引用,不然函数体里只会使用副本,外部没法更改 { int a,b; a = *ptr; b = *ptr; return a*b; } |
1
2
3
4
5
6
|
long square( volatile int *ptr) { int a; a = *ptr; return a*a; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include <iostream> #include <typeinfo> using namespace std; int main( void ) { // sample 1 cout << typeid (1.1f).name() << endl; // sample 2 class Base1 {}; class Derive1: public Base1 {}; Derive1 d1; Base1& b1 = d1; cout << typeid (b1).name() << endl; // 输出"class Base1",因为Derive1和Base1之间没有多态性 // sample 3, 编译时需要加参数 /GR class Base2 { virtual void fun( void ) {} }; class Derive2: public Base2 { }; Derive2 d2; Base2& b2 = d2; cout << typeid (b2).name() << endl; // 输出"class Derive2",因为Derive1和Base1之间有了多态性 // sample 4 class Derive22: public Base2 { }; // 指针强制转化失败后可以比较指针是否为零,而引用却没办法,所以引用制转化失败后抛出异常 Derive2* pb1 = dynamic_cast <Derive2*>(&b2); cout << boolalpha << (0!=pb1) << endl; // 输出"true",因为b2本身确实是指向Derive2 Derive22* pb2 = dynamic_cast <Derive22*>(&b2); cout << boolalpha << (0!=pb2) << endl; // 输出"false",因为b2本身不是指向Derive2 try { Derive2& rb1 = dynamic_cast <Derive2&>(b2); cout << "true" << endl; } catch ( bad_cast ) { cout << "false" << endl; } try { Derive22& rb2 = dynamic_cast <Derive22&>(b2); cout << "true" << endl; } catch ( ... ) // 应该是 bad_cast,但不知道为什么在VC++6.0中却不行?因为VC++6.0默认状态是禁用 RTTI 的,启用方式:project->setting->c/c++->category->c++ Language 下面第二个复选框选中。 { cout << "false" << endl; } return 0;} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
class Test1 { public : Test1( int n) { num=n; } //普通构造函数 private : int num; }; class Test2 { public : explicit Test2( int n) { num=n; } //explicit(显式)构造函数 private : int num; }; int main() { Test1 t1=12; //隐式调用其构造函数,成功 Test2 t2=12; //编译错误,不能隐式调用其构造函数 Test2 t2(12); //显式调用成功 return 0; } |
标签:
原文地址:http://blog.csdn.net/renchunlin66/article/details/51350933