智能指针的问题

将自己的智能指针 应用到链表中:

#ifndef LINKLIST_H
#define LINKLIST_H

#include "List.h"
#include "Exception.h"
#include "SmartPointer.h"

namespace DTLib {
template<typename T>
class LinkList :public List<T>
{
protected:
    struct Node:public Object{
        //头节点的隐患
        T value;//这里创建了T 的对象 如果这个对象(一般是类)中 有异常 会直接导致程序无法继续执行下去;
        //Node* next;
        SmartPointer<Node> next; //将原来的指针变成了智能指针
    };
    //mutable Node m_header; //为什么用这个mutable  const成员函数 中 不可以改变值 加上mutable 就可以;
    //这里为什么这样去使用    使用匿名的类型  不能调用构造函数
    //这里一定要通过继承 来同化m_header, 否则测函数接口的时候 不会成功
    mutable struct:public Object{
        char reseved[sizeof(T)];
        //Node* next;
        SmartPointer<Node> next;
    }m_header;

    int m_length;
    int m_step;
    //Node* m_current;
    SmartPointer<Node> m_current;
    //reinterpret_cast 这里要进行强制类型的转换
    Node* position(int i)const{
         SmartPointer<Node> ret =reinterpret_cast<Node*>(&m_header);
        //Node* ret =reinterpret_cast<Node*>(&m_header);
        for(int t=0;t<i;t++){
            ret=ret->next;
        }
        return ret.get();
    }
    //封装 这两个 函数 有什么用?
    virtual Node* create(){
        return new Node();
    }
    virtual void destroy(Node* pn ){
        delete pn;
    }
public:
    //构造函数 初始化成员
    LinkList(){
        m_header.next = NULL;
        m_length = 0;
        m_step = 1;
        m_current = NULL;
    }
     bool insert(const T& e){
        return insert(m_length,e);
    }

     bool insert(int i,const T& e){
         bool ret = ((i>=0)&&(i<=m_length));
         if(ret){
             Node* node = create();
             if(node!=NULL){
                 Node* current = position(i);
                 /*
                 Node* current = &m_header;
                 for(int t=0;t<i;t++){
                     current=current->next;
                 }*/
                 //插入的关键
                 node->value = e;
                 node->next =current->next;
                 current->next =node;
                 m_length++;

             }else {
                 THROW_EXCEPTION(NoEnoughMemoryException,"no node enough memory to insert");
             }
         }

         return ret;
    }
     bool remove(int i){
          bool ret = ((i>=0)&&(i<m_length));
          if(ret){
                SmartPointer<Node> current = position(i);
               //删除的关键
                SmartPointer<Node> toDel = current->next;
               if(m_current == toDel){
                   m_current=toDel->next;
               }
               current->next=toDel->next;
                m_length--;
               //destroy(toDel.get());

          }

          return ret;

    }

     bool set(int i,const T& e){
         bool ret = ((i>=0)&&(i<m_length));
         if(ret){
             Node* current = position(i);
             current->next->value = e;
         }
         return ret;

    }
     //Get 的重载 
     T get(int i){
         T ret;
         if(get(i,ret)){
             return ret;
         }else{
             THROW_EXCEPTION(IndexOutOfBoundsException,"Invalid  i in get()");
         }
 }
     bool get(int i,T& e)const{
         bool ret = ((i>=0)&&(i<m_length));
         if(ret){
           Node* current = position(i);
           e = current->next->value;

         }
         return ret;
    }

     int find(const T& e)const{
       int  ret = -1;
       int i =0;
       SmartPointer<Node> node = m_header.next;
       while(node.get()!=NULL){
           if(node->value==e){
              ret= i;
              break;
           }else{
               node = node->next;
               i++;
           }
       }
       return ret;
     }

     int length()const{
        return m_length;
    }
//遍历节点 删除所有的
     void clear(){
      while(m_header.next.isNull())
      //while(m_header.next!=NULL)
      {
          SmartPointer<Node> toDel =m_header.next;
          //Node* toDel =m_header.next;
          m_header.next=toDel->next;
          m_length--;
          //destroy(toDel.get());
      }

    }
  bool move(int i, int step=1){
      bool ret =(0<=i)&&(i<m_length)&&(0<step);
      if(ret){
          m_current  =  position(i)->next;
          m_step = step;

      }
      return ret;
  }

  bool end(){
    //return (m_current == NULL);
      return m_current.isNull();
  }

  T current(){
      if(m_current.get()!=NULL){
      return m_current->value;
      }else{
          THROW_EXCEPTION(InvalidOperationException," No value at current position  ...");
      }
  }

  bool next(){
    int i =0 ;
    while((i<m_step) && !end()){
        m_current = m_current->next;
        i++;
    }
    return (1==m_step);
  }

     ~LinkList(){
     clear();
 }
};

}

#endif // LINKLIST_H

起点是好的 但是最后运行不了 直接报错 ; 报错的原因是因为 遍历的过程中存在了 多次指向同一内存的地址 所以导致的报错(智能指针不允许存在多个指针指向同一内存);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值