文件名称:DArray-Template-better
介绍说明--下载内容均来自于网络,请自行研究使用
现在实现的类已经比较好了,用户使用起来很方便。
但是,新的问题又出来了。
上述的动态数组只能存储double类型的数据。想象一下,另一个用户也想用这个动态数组,但是他处理的数据是int类型的,该如何办?
当然,你可以再写一个基于int类型的动态数组类,你发现,大部分的代码都不要改动,只要将类型double变成int即可。
如果另一个用户需要处理的是float类型,还有用户处理的是char类型的….该怎么办?
这样做的缺点很显然:
这些类的代码大同小异,代码冗余;
当对这个类进行修改时,所有的这些类都要同时改,不利于代码维护;
有无办法解决这个问题?即,如果能把变量的“类型”也当作一个“变量”,不就可以了吗?这个在C中是不能解决的,但是在C++中,有一种新的机制template,就可以解决上述问题。Template的使用非常简单,大致的形式见:
-Now the class has been better achieved, and the user is easy to use.
However, the new issue came out again.
The dynamic array can store double types of data. Imagine, another user would like to use this dynamic array, but his handling of the data is int type, how do?
Of course, you can write a dynamic array of type int-based class, you find that most of the code have not changed, simply type int can become double.
If another user needs to be addressed is the float type, there is the user dealing with the char type .... How to do?
This disadvantage is clear:
similar code for these classes, code redundancy
When the right to modify this class, all of these classes should also change, is not conducive to code maintenance
Any other way to solve this problem? That is, if the variable can "type" also as a "variable", not to the information? This can not be solved in C, but in C++, there is a new mechanism template, you can solve these problems.
但是,新的问题又出来了。
上述的动态数组只能存储double类型的数据。想象一下,另一个用户也想用这个动态数组,但是他处理的数据是int类型的,该如何办?
当然,你可以再写一个基于int类型的动态数组类,你发现,大部分的代码都不要改动,只要将类型double变成int即可。
如果另一个用户需要处理的是float类型,还有用户处理的是char类型的….该怎么办?
这样做的缺点很显然:
这些类的代码大同小异,代码冗余;
当对这个类进行修改时,所有的这些类都要同时改,不利于代码维护;
有无办法解决这个问题?即,如果能把变量的“类型”也当作一个“变量”,不就可以了吗?这个在C中是不能解决的,但是在C++中,有一种新的机制template,就可以解决上述问题。Template的使用非常简单,大致的形式见:
-Now the class has been better achieved, and the user is easy to use.
However, the new issue came out again.
The dynamic array can store double types of data. Imagine, another user would like to use this dynamic array, but his handling of the data is int type, how do?
Of course, you can write a dynamic array of type int-based class, you find that most of the code have not changed, simply type int can become double.
If another user needs to be addressed is the float type, there is the user dealing with the char type .... How to do?
This disadvantage is clear:
similar code for these classes, code redundancy
When the right to modify this class, all of these classes should also change, is not conducive to code maintenance
Any other way to solve this problem? That is, if the variable can "type" also as a "variable", not to the information? This can not be solved in C, but in C++, there is a new mechanism template, you can solve these problems.
(系统自动生成,下载前可以参看下载内容)
下载文件列表
DArray-Template-better\DynamicArray.dsw
......................\DynamicArray.ncb
......................\DynamicArray.plg
......................\DynamicArray.dsp
......................\.ebug\vc60.idb
......................\.....\vc60.pdb
......................\.....\DynamicArray.exe
......................\.....\DynamicArray.pdb
......................\.....\main.obj
......................\.....\DynamicArray.pch
......................\.....\DArray.obj
......................\.....\DynamicArray.ilk
......................\DArray.cpp
......................\main.cpp
......................\DArray.h
......................\DynamicArray.opt
......................\Debug
DArray-Template-better
......................\DynamicArray.ncb
......................\DynamicArray.plg
......................\DynamicArray.dsp
......................\.ebug\vc60.idb
......................\.....\vc60.pdb
......................\.....\DynamicArray.exe
......................\.....\DynamicArray.pdb
......................\.....\main.obj
......................\.....\DynamicArray.pch
......................\.....\DArray.obj
......................\.....\DynamicArray.ilk
......................\DArray.cpp
......................\main.cpp
......................\DArray.h
......................\DynamicArray.opt
......................\Debug
DArray-Template-better