曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3346|回复: 1
打印 上一主题 下一主题

通用类中使用标准类型参数注意

[复制链接]

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
跳转到指定楼层
楼主
发表于 2011-11-19 02:05:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在通用类的模板定义中,也可以使用标准类型的参数。也就是说,在模板的定义中,你可以指定标准类型参数。

测试代码:
[C++] 纯文本查看 复制代码
#include <iostream>
#include <cstdlib>
using namespace std;

//size 是一个标准类型的参数
template <class AType, int size> class atype
{
        AType a[size];  //通过参数 size 传递数组的长度
public:
        atype()
        {
                register int i;
                for (i = 0; i < size; i++)
                        a[i] = i;
        }

        AType &operator[] (int i);
};

//为 atype 类型的对象提供边界检测
template <class AType, int size> AType &atype<AType, size>::operator[](int i)
{
        if (i < 0 || i > size-1) {
                cout << "\nIndex value of ";
                cout << i << " is out-of-bounds.\n";
                exit(1);
        }
        return a[i];
}

int main()
{
        atype<int, 10> intob;           //大小为10的整数数组对象
        atype<double, 15> doubleob;     //大小为15的double数组对象

        int i;

        cout << "Integer array: ";
        for (i = 0; i < 10; i++)
                intob[i] = i;

        for (i = 0; i < 10; i++)
                cout << intob[i] << " ";
        cout << '\n';

        cout << "Double array: ";
        for (i = 0; i < 15; i++)
                doubleob[i] = (double) i/3;
        for (i = 0; i < 15; i++)
                cout << doubleob[i] << " ";

        cout << '\n';

        return 0;
}


运行输出:
Integer array: 0 1 2 3 4 5 6 7 8 9
Double array: 0 0.333333 0.666667 1 1.33333 1.66667 2 2.33333 2.66667 3 3.33333 3.66667 4 4.33333 4.66667
说明
在 atype 模板定义中,参数 size 被声明为 int 类型,它用来指定数组 a 的大小。尽管 size 在源代码中是一个“变量”,但她的值是在编译时得到的,这样就可以用来设置数组的大小。

在模板定义中使用的参数的标准类型仅限于整数类型,指针类型或者引用类型,而其它的数据类型(如 float)是不允许作为模板定义中参数的标准类型的。传递给模板的实际参数必须要么包含一个整数常量,要么包含一个全局函数或全局对象的指针或引用。也就是说,由于标准类型参数的值不能被改变,所以标准类型参数也就可以认为是常量。

比如下面代码:
[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;

template <class Atype, float size> class atype
{
public:
        atype() {
                cout << size << endl;
        }
};

int main()
{
        atype<int, 100> a;

        return 0;
}

注意,在上面的模板类的定义中,故意将 int size 改成 float size 后,编译器会发出错误信息:
temp.cc:5:30: error: ‘float’ is not a valid type for a template constant parameter                     
temp.cc: In constructor ‘atype<Atype, <declaration error> >::atype()’:                                 
temp.cc:9:11: error: ‘size’ was not declared in this scope                                             
temp.cc: In function ‘int main()’:                                                                     
temp.cc:15:16: error: ‘<type error>’ is not a valid type for a template constant parameter            
temp.cc:15:19: error: invalid type in declaration before ‘;’ token            

又如我们将上面的 size 进行赋值操作:
[C++] 纯文本查看 复制代码
template <class Atype, int size> class atype
{
public:
        atype() {
                cout << size << endl;
                size = 200;
                cout << size << endl;
        }
};

编译时会看到下面的错误提示:
temp.cc: In constructor ‘atype<Atype, size>::atype() [with Atype = int, int size = 100]’:              
temp.cc:17:18:   instantiated from here                                                               
temp.cc:10:3: error: lvalue required as left operand of assignment  
从上面的提示信息知道:左值需要为其分配一个左操作数。由此可见,size 并不是一个合适的左操作数,正如上面所讲,它是一个常量,而常量是不能给它赋值的。

由于标准类型参数可以看做是常量,所以他们可以用来设置数组的大小,这是一个很实在的好处。

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
沙发
 楼主| 发表于 2011-11-19 11:48:44 | 只看该作者

模板类使用默认参数

模板类可以有一个通用类型的默认参数,比如:
[C++] 纯文本查看 复制代码
template <class X=int> class myclass { //... }; 

如果在实例化一个 myclass 对象时没有指定数据类型,那么 int 就作为默认的数据类型。

标准类型参数也可以使用默认参数值。在实例化类时如果没有显式地为标准类型参数指定参数值,那么参数将使用默认值。

测试代码
[C++] 纯文本查看 复制代码
#include <iostream>
#include <cstdlib>
using namespace std;

//类型参数AType默认是int类型,数组的大小默认为10
template <class AType = int, int size = 10> class atype {
        AType a[size];  //通过参数size传递数组长度
public:
        atype() {
                register int i;
                for (i = 0; i < size; i++)
                        a[i] = i;
        }
        AType &operator[](int i);
};

//为atype类型的对象提供边界检测
template <class AType, int size> AType &atype<AType, size>::operator[](int i)
{
        if (i < 0 || i > size - 1) {
                cout << "\\nIndex value of ";
                cout << i << " is out-of-boundss.\\n";
                exit(1);
        }
        return a[i];
}

int main()
{
        atype<int, 100> intarray;       //大小为100的整数数组对象

        atype<double> doublearray;      //默认大小的double数组对象

        atype<> defarray;       //默认大小为10的整数数组对象

        int i;


        cout << "int array: ";
        for (i = 0; i < 10; i++)
                intarray[i] = i;
        for (i = 0; i < 10; i++)
                cout << intarray[i] << " ";
        cout << '\\n';

        cout << "double array: ";
        for (i = 0; i < 10; i++)
                doublearray[i] = (double) i/3;
        for (i = 0; i < 10; i++)
                cout << doublearray[i] << " ";
        cout << '\\n';

        cout << "defarray array: ";
        for (i = 0; i < 10; i++)
                defarray[i] = i;
        for (i = 0; i < 10; i++)
                cout << defarray[i] << " ";
        cout << '\\n';

        return 0;
}

运行输出:
int array: 0 1 2 3 4 5 6 7 8 9
double array: 0 0.333333 0.666667 1 1.33333 1.66667 2 2.33333 2.66667 3
defarray array: 0 1 2 3 4 5 6 7 8 9

注意上面代码中的 template <class AType=int, int size = 10> class atype { ... }; 在这行代码中,类型参数 AType 默认时位 int 型,而整数参数 size 默认值为 10 。在程序中,我们通过 3 种方法创建了 AType 类型对象:
1. 显式地指定数组的数据类型和大小。
2. 显式指定数组的数据类型,而数组大小默认为 10 。
3. 数组的数据类型默认为 int,大小也默认为 10 。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-17 21:46 , Processed in 0.070173 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表