有Template的专家吗?

Ju 2000-09-01 09:17:00
我想声明和使用自己的Template,但不知道如何做,愿请教各位。另,有无这方面的资料?
...全文
132 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
DUKE_SOFT 2000-09-05
  • 打赏
  • 举报
回复
// in the *.h, specify the typename's data type, such as char, int, and
// a class with the default constructor, a copy constructor, an assignment
// operator.
....
template <typename Item>

class A:
{

};


// in the *.ipp
template <typename Item>
A::...

// in the main.cpp
A<char> A1;

// some c++ books.
Kevin_qing 2000-09-03
  • 打赏
  • 举报
回复
//Array.cpp
#include "../require.h"
#include <iostream>
using namespace std;

template<class T>
class Array {
static const int size = 100;
T A[size];
public:
T& operator[](int index) {
require(index >= 0 && index < size,
"Index out of range");
return A[index];
}
};

int main() {
Array<int> ia;
Array<float> fa;
for(int i = 0; i < 20; i++) {
ia[i] = i * i;
fa[i] = float(i) * 1.414;
}
for(int j = 0; j < 20; j++)
cout << j << ": " << ia[j]
<< ", " << fa[j] << endl;
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧