C++沉思录 13章的有关问题(作者并没有解决暴露类内部的实现机制?)

C++沉思录 13章的问题(作者并没有解决暴露类内部的实现机制?)
本帖最后由 mmhaojie 于 2012-12-12 11:30:42 编辑
用户能够轻易的获得指向Vector内部的指针,这种关系暴露了类内部的实现机制?
 T& operator[](unsigned n) const             //读
              {
                     if(n >= sz || data == 0)
                            throw "Vector subscript out of range";
                     return data[n];
              }
//改为

 T operator[](unsigned n) const             //讀
              {
                     if(n >= sz || data == 0)
                            throw "Vector subscript out of range";
                     return data[n];
              }
//用update 函数来修改Array中的元素


可以理解,作者是为了禁止T* t=&*p(也就是防止暴露类内部的实现机制,不是很理解)  其中p的定义为Point<T> p

最重要的一点:
13章并没有解决他所说的暴露类内部的实现机制问题!如最后 的实现是这样的:

  Pointer& operator=(const Pointer<T> &p)
              {
                     if(p.ap)
                            ++p.ap->use;
                     if(ap && --ap->use == 0)
                            delete ap;
                     ap = p.ap;
                     sub = p.sub;
                     return *this;
              }
              T& operator*() const
              {
                     if(ap == 0)
                            throw "* of unbound Pointer";