CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

关于more effective c++上的Proxy Class的一问。

楼主zjpixyniannian()2004-09-01 16:56:52 在 C/C++ / C++ 语言 提问

 
   
  #include   <iostream>  
  using   namespace   std;  
   
  template<class   T>  
  class   Array  
  {  
  public:  
  class   Proxy  
  {  
  public:  
  Proxy(Array<T>&   array,   int   index)  
  :   theArray(array),   itemIndex(index)  
  {}  
   
  Proxy&   operator   =   (const   T&   rhs)  
  {  
  theArray.array[itemIndex]   =   rhs;  
  return   *this;  
  }  
  operator   T()   const  
  {  
  return   theArray.array[itemIndex];  
  }  
   
  private:  
  Array&   theArray;  
  int   itemIndex;  
  };  
   
  const   Proxy   operator[](int   index)   const;  
  Proxy   operator[](int   index);  
   
  friend   class   Proxy;  
  Array(int   dim);  
  ~Array();  
   
  private:  
  T*   array;  
  };  
   
  template<class   T>  
  Array<T>::Array(int   dim)  
  {  
          array   =   new   T[dim];  
  }  
   
  template<class   T>  
  Array<T>::~Array()  
  {  
          delete   []   array;  
  }  
   
  template<class   T>  
  Array<T>::Proxy   Array<T>::operator[](int   index)  
  {  
          return   Proxy(*this,   index);  
  }  
   
  template<class   T>  
  const   Array<T>::Proxy   Array<T>::operator   []   (int   index)   const  
  {  
          return   Proxy(const_cast<Array&>(*this),   index);  
  }  
   
  class   Rational  
  {  
  public:  
  Rational(int   numerator   =   0,   int   denominator   =   1);  
  int   numerator()   const;  
  int   denominator()   const;  
  friend   ostream&   operator<<(ostream&   os,   const   Rational&   rhs);  
   
   
  private:  
  int   num;  
  int   deno;  
  };  
   
  Rational::Rational(int   numerator,   int   denominator)  
  :   num(numerator),   deno(denominator)  
  {  
          std::cout   <<   "Rational   Construct"   <<   std::endl;  
  }  
   
  int   Rational::numerator()   const  
  {  
          std::cout   <<   "numerator"   <<   std::endl;  
          return   0;  
  }  
   
  int   Rational::denominator()   const  
  {  
          std::cout   <<   "denominator"   <<   std::endl;  
          return   0;  
  }  
   
  ostream&   operator<<(ostream&   os,   const   Rational&   rhs)  
  {  
          os   <<   rhs.num   <<   "   "   <<   rhs.deno;  
          return   os;  
  }  
   
  int   main()  
  {  
          Array<Rational>   a(5);  
          a[1].numerator();   //错误  
  }  
   
  错误的原因是:  
  operator[]返回一个代理对象而不是实际的Rational对象。但成员函数numerator()和denominator()只存在于Rational对象上,而不是其代理对象。  
  但是我想为什么这里编译器不会自己去调用隐式转换函数  
  operator   T()   const  
  {  
          return   theArray.array[itemIndex];  
  }  
  呢?  
  如果能够调用,那么应该就不会出现错误了。为什么不会调用呢? 问题点数:20、回复次数:3Top

1 楼grooving()回复于 2004-09-01 17:14:10 得分 6

int   main()  
  {  
          Array<Rational>   a(5);  
          ((Rational)a[1]).numerator();   //错误  
  }  
   
  我只会写。具体的我也说不明白。Top

2 楼goodboy1881(积木)(谁都别拦着我在水源升星)回复于 2004-09-01 17:23:26 得分 7

C++再怎么聪明也不至于聪明到这个程度啊,它是不知道你想做什么的要你指一条路啊  
  你现在所需要的就是一个强制转换啊,那个强制转换的定义,不是为了编译器做强制转换操作的时候使用的,而是你要写强制转换的时候使用的~Top

3 楼zfluo(云淡风清)回复于 2004-09-01 17:29:44 得分 7

这个是一定要显式调用的,举个例子  
  如果类里面有一个  
  operator   A()    
  {  
      return   a_;  
  }  
  operator   B()  
  {    
      return   b_;  
  }  
  那么你让编译器隐式调用哪一个呢?Top

相关问题

  • Effective C++
  • 《Thinking in C++》《Essential C++》《Effective C++》
  • memory pool class地实现,effective c++条款10的一个问题
  • more effective c++
  • 关于《effective c++》和 《more effective c++》...
  • Effective C++ 是什么?
  • c++ static class 的意思
  • 哪有Effective C++的下载?
  • 哪有《More Effective C++》下载???
  • 高分求书, More Effective C++

关键词

  • 编译器
  • 转换
  • rational
  • template
  • proxy
  • 调用
  • thearray
  • array
  • itemindex
  • denominator

得分解答快速导航

  • 帖主:zjpixyniannian
  • grooving
  • goodboy1881
  • zfluo

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo