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

Destructor For A Dynamic Array [C++]

我现在正在处理一个动态数组问题,并且已经为它创建了一个析构函数。代码编译得很好,但是因为我没有提供测试文件,所以我不确定析构函数是否完全可用。如果我在正确的轨道上,谁能告诉我?为了便于阅读,我已经包含了完整的 header 和 .cpp,并删除了一些不相关的函数。析构函数指定为 // This is the question // in .cpp in the function。任何帮助表示赞赏!

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;                                  
}

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

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;                                           
}