析构函数中的析构函数?

吉布斯

我正在尝试按对象删除分配的内存,但是它是以链接列表的形式。有人可以提出建议吗?

这是我的头文件

class XKey
{   
public:
    XKey();
    virtual ~XKey();

private:
    char *m_name;
    char *m_value;
    XKey *m_next;
};

class XSection
{   
public:
    XSection();
    virtual ~XSection();  

private:    
    char *m_name;
    XKey *m_keys;
    XSection *m_next;
};

class XIniFile
{
public:
    XIniFile();
    virtual ~XIniFile();

private:
    char *m_name;
    XSection *m_sections;
};

这是我的程序文件

XKey::~XKey()
{
    delete(m_name);
    delete(m_value);
    m_next = 0;
}

XSection::~XSection()
{
    XKey k;
    XKey ks;

    k = m_keys;
    while (k){
        ks = k;
        k = k->getNext();
        //////////////<<<--- How can I call a destructor here from XKey?
        delete(m_name);
        m_keys = 0;
        m_next = 0;
    }
}

XIniFile::~XIniFile()
{
    XSection *sec;
    XSection *sp;

    sec = m_sections;
    while (sec) {
        sp = sec;
         //////////////<<<--- How can I call a destructor here from XSection?
        delete(m_name);
        m_sections = 0;
    }    

}

我那里有一些错别字,但请重点介绍如何在析构函数内调用析构函数。谢谢!

异域
XSection::~XSection()
{
    XKey* k = m_keys;   // must be a pointer!

    while (k){
        XKey* ks = k;   // must be a pointer!
        k = k->getNext();
        if (ks != nullptr) delete ks;
    }
    delete [] m_name; // allocated with new[]?
}

这是非常c ++ 98,您应该考虑拥抱c ++ 11(智能指针)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章