template<class T,int Size) 后面的int Size有上面作用
时间: 2024-04-04 07:30:52 浏览: 16
`template<class T, int Size>`中的`int Size`是一个非类型模板参数,它表示的是一个整型常量,用于在编译期间进行计算和判断。非类型模板参数可以是整型、枚举、指针或引用类型,但不能是浮点数、类类型和数组类型。在使用这样的模板时,需要在模板实例化时指定非类型模板参数的值,例如`MyArray<int, 10>`表示一个由10个int类型元素组成的数组。这样可以在编译期间进行类型检查和优化,提高程序的效率和安全性。
相关问题
template<class T, int SIZE = 20>
这是一个模板类的定义,其中T是一个类型参数,SIZE是一个非类型参数,它的默认值是20。这个模板类可以用来定义一个大小固定为SIZE的数组,数组元素类型为T。在使用时,可以根据需要指定T的具体类型和数组的大小,例如:template<class T, int SIZE = 20> class MyArray; MyArray<int, 10> arr; 表示定义了一个大小为10的int类型数组。
template<class T,int OTER_SIZE> friend class Stack<T, OTER_SIZE>;友元模板声明无效
如果在类中声明了友元模板类,那么它的模板参数列表需要与被声明类的模板参数列表完全一致,并且需要在类外部再次声明一遍。例如,如果我们在`Stack`类中声明了一个友元模板类`OtherStack`,那么我们需要这样写:
```
template<class T, int OTHER_SIZE>
class OtherStack;
template<class T, int SIZE>
class Stack {
public:
// ...
private:
// ...
template<class T, int OTHER_SIZE>
friend class OtherStack;
};
```
注意,这里的`OtherStack`类模板声明需要在类外部进行,而且模板参数列表需要与类内部声明的完全一致。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)