C++类模板中的非类型参数

#include<iostream>
using namespace std;
//类模板的<类Y参数表”中可以出现非类型参数

template<class T,int size>
class test
{
	T array[size];
public:
	test() {
		cout << "constructor was called!" << endl;
	}
	T* getArray()
	{
		return array;
	}
	void print()
	{
		cout << "array:" << endl;
		for (int i = 0; i < size; i++)
		{
			cout << array[i] << " ";
		}
		cout << endl;
	}
};
int main()
{
	/*对类模板进行实例化:test<float,40>,得到模板类(名):test<float,40>*/
	test<float,3> n;
	float k = 1.11;
	for (int i = 0; i < 3; i++)
	{
		n.getArray()[i] = k * i;
	}
	n.print();

}
  • List item
posted @ 2024-10-04 23:45  xuchaoxin1375  阅读(7)  评论(0)    收藏  举报  来源