68-拾遗:令人迷惑的写法

来源:互联网 发布:小学英语单词软件 编辑:程序博客网 时间:2024/05/20 14:43

1、问题1

这里写图片描述

2、

这里写图片描述

int a = 0;class Test_1{public:    static const int TS = 1;};class Test_2{public:    struct TS    {        int value;    };};template< class T >void test_class(){                               //两种解读方式    typename T::TS * a;        // 1. 通过泛指类型 T 内部的数据类型 TS 定义指针变量 a (推荐的解读方式)                               // 2. 使用泛指类型 T 内部的静态成员变量 TS 与全局变量 a 进行乘法操作}int main(int argc, char *argv[]){    test_class<Test_1>();    //error: no type named ‘TS’ in ‘class Test_1’     typename T::TS * a;    //在Test_1的内部没有TS类型,只有变量,编译器默认认为TS为类型    test_class<Test_2>();//OK    return 0;}

3、

这里写图片描述

4、

这里写图片描述

5、问题2

这里写图片描述

6、

这里写图片描述

#include <iostream>#include <string>using namespace std;//throw表示函数异常声明,这个函数可能抛出异常的类型都要显示指出,//若不指定异常类型throw(),则抛出的异常返回给上层,最终导致崩溃//terminate called after throwing an instance of 'char'//Aborted (core dumped)int func(int i, int j) throw(int, char){    if( (0 < j) && (j < 10) )    {        return (i + j);    }    else    {        throw '0';    }}void test(int i) try{    cout << "func(i, i) = " << func(i, i) << endl;}catch(int j){    cout << "Exception: catch(int j)" << i << endl;}catch(...){    cout << "Exception..." << endl;}int main(int argc, char *argv[]){    test(5);    test(10);    return 0;}func(i, i) = 10Exception...

7、函数异常声明

这里写图片描述

8、小结

这里写图片描述

原创粉丝点击