template使用

发布时间 2023-11-15 17:16:58作者: 一名博客

template语法

template <typename T>
类/函数的实现

注意:

  1. typename 可以指定int,float等内置数据类型,自定义的class
  2. 模板只有再使用的时候才会定义
  3. 模板的定义不能与标准库冲突

template用法

重载的时候

//打印不同的数据类型
//print(5)
//print(5.0f)
//print("hello world!")

template<typename T>
void print (T value) {
  std::cout << value << std::endl;
}

动态数组的建立

template <typename T,int size>
class MyArray {
private:
  T array[size];
pubilc:
  int GetArraySize() {return size;}
};
//再创建的时候要指定size的大小
//MyArray<int, 10> array;

持续更新......