问题1:
下面的程序表达什么意思?
Class 和typename一样,原因如下
template < class T >
class Test
{
public:
Test(T t)
{
cout << "t = " << t << endl;
}
};
template < class T >
void func(T a[], int len)
{
for(int i=0; i<len; i++)
{
cout << a[i] << endl;
}
}
//
//
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>();
test_class<Test_2>();
return 0;
}
//如果通过分别调用
//如果用模板调用Test1 和 test2 产生的效果是不一样的
问题2:
分析下面的程序
int func(int i) try
{
return i;
}
catch(...)
{
return -1;
}
//分析
//将函数分隔成两部分
int func(int i, int j)
throw(int)
{
return i + j;
}
//函数异常规格说明
//异常规格说明只能对应的类型
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 i)
{
cout << "Exception: " << i << endl;
}
catch(...)
{
cout << "Exception..." << endl;
}
int main(int argc, char *argv[])
{
test(5);
test(10);
return 0;
}