我的循环链接列表中的remove方法是否定义良好?

问题描述 投票:0回答:1

我正在建立一个循环链表,我想知道do_remove方法是否定义明确。当我运行该程序时,它向我显示它是,但是,我仍然有点困惑,为什么我不需要在这种情况下的虚拟destrutor。

当我想通过它的基指针销毁派生类时,是否只需要虚拟析构函数?

这是否意味着通过将基类向下转换为派生类,然后调用派生类析构函数,它将始终调用基类析构函数?

do_remove方法可能存在泄漏吗?

PS:我需要对象创建和销毁是一个两步过程 - 分配/调用构造函数/调用析构函数/释放;这就是为什么我暂时使用::operator new

这是我正在编写的代码的自包含示例:

#include <iostream>

struct NodeBase {
    NodeBase * previous;
    NodeBase * next;

    NodeBase() noexcept
    : previous(this)
    , next(this) {
    }

    NodeBase(NodeBase * const previous, NodeBase * const next) noexcept
    : previous(previous)
    , next(next) {
    }

    ~NodeBase() {
        std::puts("~NodeBase()");
    }
};

template <typename TYPE>
struct Node : NodeBase {
    TYPE data;

    template <typename ...ARGUMENTS>
    Node(NodeBase * const previous, NodeBase * const next, ARGUMENTS && ...arguments)
    : NodeBase(previous, next)
    , data(std::forward<ARGUMENTS>(arguments)...) {
        previous->next = this;
        next->previous = this;
    }

    ~Node() {
        std::puts("~Node()");
    }
};

template <typename TYPE>
class List {
    using Node = Node<TYPE>;

    int64_t this_length;
    NodeBase this_sentinel;

    Node * as_node(NodeBase * const input) noexcept {
        return static_cast<Node * const>(input);
    }

    Node const * as_node(NodeBase const * const input) const noexcept {
        return static_cast<Node const * const>(input);
    }

    template <typename ...ARGUMENTS>
    List & do_insert(NodeBase * const node, ARGUMENTS && ...arguments) {
        void * const address = ::operator new(sizeof(Node));
        try {
            new (address) Node(node->previous, node, std::forward<ARGUMENTS>(arguments)...);
            ++this_length;
            return *this;
        } catch (...) {
            ::operator delete(address);
            throw;
        }
    }

    // Is this method well defined?
    List & do_remove(NodeBase * input) noexcept {
        Node * const node = as_node(input);
        input->previous->next = input->next;
        input->next->previous = input->previous;
        node->~Node();
        ::operator delete(node);
        --this_length;
        return *this;
    }

public:
    List()
    : this_length(0)
    , this_sentinel() {
    }

    ~List() {
        std::puts("~List()");
        while (this_length) {
            pop();
        }
    }

    TYPE & head() noexcept {
        return as_node(this_sentinel.next)->data;
    }

    TYPE const & head() const noexcept {
        return as_node(this_sentinel.next)->data;
    }

    TYPE & last() noexcept {
        return as_node(this_sentinel.previous)->data;
    }

    TYPE const & last() const noexcept {
        return as_node(this_sentinel.previous)->data;
    }

    template <typename ...ARGUMENTS>
    List & push(ARGUMENTS && ...arguments) {
        return do_insert(this_sentinel.next, std::forward<ARGUMENTS>(arguments)...);
    }

    List & pop() noexcept {
        return do_remove(this_sentinel.next);
    }
};

int main() {

    List<int> list;

    list.push(5).push(7).push(3);

    std::cout << list.head() << std::endl;
    std::cout << list.last() << std::endl;

    return 0;
}
c++ linked-list destructor doubly-linked-list circular-list
1个回答
1
投票

当我想通过它的基指针销毁派生类时,是否只需要虚拟析构函数?

是。只有当您使用基指针删除对象时(或者如果您使用unique_ptr来管理它),您需要在基础中使用虚拟析构函数。其他情况,例如删除指向大多数派生类型的指针或使用'shared_ptr'管理到base不需要虚拟析构函数。

这是否意味着通过将基类向下转换为派生类,然后调用派生类析构函数,它将始终调用基类析构函数?

是。派生类的析构函数需要调用(基础和成员)子对象的析构函数。

do_remove方法中是否可能存在泄漏?

不,如果TYPE的析构函数不泄漏。为简单起见,最好使用普通的删除表达式

    delete node;

而不是写出它无论如何要做的事情

    node->~Node();
    ::operator delete(node);
© www.soinside.com 2019 - 2024. All rights reserved.