模板及运算符一个例子
2011-01-27 09:44
281 查看
模板及运算符重载的一个小例子 供学习
#include "stdafx.h"
#include <assert.h>
#include <iostream>
using namespace std;
template<class T> class array//模板数组类
{
enum {size = 100};
T A[size];
public:
T& operator[] (int index);//运算符 这里设为引用返回类型 由于可读可写 所以不设置为const 类型
};
template<class T> T& array<T>::operator[] (int index)
{
assert(index >= 0 && index <size);
return A[index];
}
void 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;
}
getchar();
};
#include "stdafx.h"
#include <assert.h>
#include <iostream>
using namespace std;
template<class T> class array//模板数组类
{
enum {size = 100};
T A[size];
public:
T& operator[] (int index);//运算符 这里设为引用返回类型 由于可读可写 所以不设置为const 类型
};
template<class T> T& array<T>::operator[] (int index)
{
assert(index >= 0 && index <size);
return A[index];
}
void 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;
}
getchar();
};
相关文章推荐
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第4章(估计要写9章)
- jct 模板通过json 从java 之间得到值的一个简单例子
- DataGrid动态添加模板列的一个例子
- 关于模板编程的一个例子 来自深度探索c++对象模型
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第一章(估计要写9章)
- 项目里面的一个下载模板并上传例子
- c++中一个关于模板的小例子
- DataGrid动态添加模板列的一个例子
- 一个c++模板的例子(类型依赖)说明
- DataGrid动态添加模板列的一个例子 http://www.cnblogs.com/lovecherry/archive/2005/03/25/125526.html
- 一个javascript模板的例子
- c++ 模板的一个例子,实参演绎的时候,有个地方没看明白
- DataGrid动态添加模板列的一个例子
- DataTemplate——数据模板的一个典型例子
- Soy文件生成JS文件 - 一个使用Google soy模板的例子
- 针对重载函数调用运算符的一个使用的例子,也就是重载()这个括号
- 一个复选框的例子(与运算符和或运算符(&和|))
- 关于运算符的一个例子
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第2章(估计要写9章)