动态数组的析构函数[C ++]

时间:2017-02-24 04:12:28

标签: c++ destructor dynamic-arrays

我现在处理动态数组问题,并为它创建了一个析构函数。代码编译得很好,但由于我还没有提供测试文件,我实际上并不确定析构函数是否完全正常。如果我走在正确的轨道上,是否有人能够告诉我?我已经完整地包含了我的标题,并且.cpp删除了一些不相关的函数以便于阅读。用//指定的析构函数这是//。在.cpp中的函数。任何帮助表示赞赏!

dynamic_array.h

using namespace std;

class Dynamic_array {
public:
    Dynamic_array();
    Dynamic_array(Dynamic_array &);
    Dynamic_array &operator=(Dynamic_array &);
    ~Dynamic_array();

    void print_state(void);
    int get_size(void);

    int& operator[](int);

    void insert(int, int);
    void insert(Dynamic_array &, int);

    void remove(int);
    void remove(int, int);

    class Subscript_range_exception {
    };
private:
    enum {
        BLOCK_SIZE = 5,
    };

    class Block {
    public:
        int size;
        int a[BLOCK_SIZE];
        Block* next_p;
    };

    class Block_position {
    public:
        Block* block_p;
        Block* pre_block_p;
        int i;
    };

    Block_position find_block(int i);
    void insert_blocks(Block *, Block *);
    void remove_blocks(Block *, Block *, Block *);
    Block * copy_blocks(Block *);
    void delete_blocks(Block *);

    int size;
    Block* head_p;
};

dynamic_array.cpp

#include <iostream>                                     
#include <string.h>                                     
#include "dynamic_array.h"                                  

using namespace std;                                        

// ********** public functions **********                           

Dynamic_array::Dynamic_array() {                                
    head_p = NULL;                                      
    size = 0;                                       
}                                               

Dynamic_array::Dynamic_array(Dynamic_array & d) {                       
    size = d.size;      
    head_p = copy_blocks(d.head_p); 
}                                               


Dynamic_array &Dynamic_array::operator=(Dynamic_array & d) {                    
    if (this != &d){
        this->head_p = copy_blocks(d.head_p);   
        this->size = d.size;
    }

    return *this;                                           
}                                               

Dynamic_array::~Dynamic_array() {   // This is the function in question //                      
 if(head_p != NULL){
        while (1) {                                                                 
        Block * p = head_p->next_p;                         
        delete head_p;                                                                      
        // advance                                  
        if (p == NULL) {                                
            break;                                  
        } else {                                    
            head_p = p;                             
        }                                       
    }
 }
}                                                                                                                                                                                           

// ********** private functions **********                                                                                                                          

// purpose                                          
//  create a new linked list which is a copy of the list pointed to p           
//  return a pointer to the head of the new linked list                 
// preconditions                                        
//  p is the head of a possibly empty linked list of blocks                 
Dynamic_array::Block * Dynamic_array::copy_blocks(Block * p) {                  
    Block * new_head_p = NULL;                              
    Block * new_p;                                      
    while (p != NULL) {                                 
        // allocate and link in new block                       
        if (new_head_p == NULL) {                           
            new_p = new Block;                          
            new_head_p = new_p;                         
        } else {                                    
            new_p->next_p = new Block;                      
            new_p = new_p->next_p;                          
        }                                       

        // copy the elements                                
        new_p->size = p->size;                              
        for (int i = 0; i < p->size; i++) {                     
            new_p->a[i] = p->a[i];                          
        }                                       

        // advance                                  
        p = p->next_p;                                  
    }                                           

    // terminate new list                                   
    if (new_head_p != NULL) {                               
        new_p->next_p = NULL;                               
    }                                           

    return new_head_p;                                  
}

1 个答案:

答案 0 :(得分:3)

您可以通过这种方式简化析构函数。

Dynamic_array::~Dynamic_array() {   // This is the function in question //
    while (head_p != NULL) {
        Block *p = head_p;
        head_p = head_p->next_p;
        delete p;
    }                      
}

旁注:

复制构造函数和赋值运算符应该有const个参数

Dynamic_array::Dynamic_array(const Dynamic_array & d) {                       
    size = d.size;      
    head_p = copy_blocks(d.head_p); 
}                                               


Dynamic_array &Dynamic_array::operator=(const Dynamic_array & d) {                    
    if (this != &d){
        this->head_p = copy_blocks(d.head_p);   
        this->size = d.size;
    }

    return *this;                                           
}