05·02 a question about template class in C++

jazz 发表于 2007-05-02 21:37:31

    Here, as we know, the generic programming has become a trend in the programming langauge design. C++ offers a good support to generic programming from its very begenning. The template methodology is such an example. You can write a template function to adopt variant type parameters. You can design a template class to have a common interface and relealize the dream of "write once and use much times". The STL which means standard Template Library and the algorithms are the applications of such methodology.

    But in VC++, the template class's declaration and defenition that is often the source code must be in the same file, or else it will not pass compile progress, and the compile error message is external symbols can not be resolved, just like we use a external variant in the function without using external directive for the very variant. I remember this question happened before when I was a undergraduate. But the template function has no such question. Surprised? Maybe.

    Below is just a sample, you can try it if you are interested in it:
//stu.h
namespace jazz
{
 template<typename Type> class Array
 {
 public:
  Array(int size_);
  ~Array() throw();
  Type& operator[](int index);
  void dump();
 private:
  Type* data;
  size_t size;
 };

 template<typename Type> Array<Type>::Array(int size_) try: size(size_),data(new Type[size_])
 {
 }catch(std::bad_alloc& e)
 {
  throw;
 }
 template<typename Type> Type& Array<Type>::operator [](int index)
 {
  if(index<0 || index>= size)
   throw std::out_of_range("script index is out of range");
  return data[index];
 }

 template<typename Type> Array<Type>::~Array() throw()
 {
  delete[] data;
 }
 template<typename Type> void Array<Type>::dump()
 {
  for(int i=0;i<size;i++)
   cout<<data[i]<<endl;
 }
}
//there is no error above
------------------------------------------
//stu.h
namespace jazz
{
 template<typename Type> class Array
 {
 public:
  Array(int size_);
  ~Array() throw();
  Type& operator[](int index);
  void dump();
 private:
  Type* data;
  size_t size;
 };
}
//stu.cpp
namespace jazz
{
template<typename Type> Array<Type>::Array(int size_) try: size(size_),data(new Type[size_])
 {
 }catch(std::bad_alloc& e)
 {
  throw;
 }
 template<typename Type> Type& Array<Type>::operator [](int index)
 {
  if(index<0 || index>= size)
   throw std::out_of_range("script index is out of range");
  return data[index];
 }

 template<typename Type> Array<Type>::~Array() throw()
 {
  delete[] data;
 }
 template<typename Type> void Array<Type>::dump()
 {
  for(int i=0;i<size;i++)
   cout<<data[i]<<endl;
 }
}
//there is compile error above

关键词(Tag): c++ template


收藏: QQ书签 del.icio.us 订阅: Google 抓虾

最新评论

发表评论

* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 
 

分类小组论坛
杂谈, 娱乐、八卦, 文学、艺术, 体育, 旅游、同城, 象牙塔, 情感, 时尚、生活, 星座, 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定