【问题标题】:How to call to the copy constructor from the copy-assignment operator?如何从复制赋值运算符调用复制构造函数?
【发布时间】:2013-06-09 02:33:40
【问题描述】:

我正在实现一个链表。我写了一个拷贝构造函数:

        // --- copy constructor ---
    IntList(const IntList& last_list) {
        first = new IntNode(last_list.getFirst()->getData());
        cout << "copy first " << first->getData() << endl;

        IntNode* last_node = first;
        IntNode* node = last_list.getFirst()->getNext();
        while(node!=NULL) {
            IntNode* new_node = new IntNode(node->getData());
            last_node->setNext(new_node);
            cout << "copy " << new_node->getData()<< endl;
            last_node = new_node;
            node = node->getNext();
        }
    }

据我了解,我的复制赋值运算符 (operator=) 应该有 2 个目标:

  • 删除当前列表。
  • 复制新列表。

我可以通过调用我已经编写的析构函数然后调用复制构造函数来实现这两个目标。我该怎么做?

【问题讨论】:

  • 什么是赋值构造函数?
  • 你的意思是它的复制赋值运算符。而且 AFAIK,您不能根据复制构造函数来定义它。
  • @MikeVine:当然可以。复制 assignemtn 的常见复制和交换通过复制构造函数工作
  • 我看到你已经编辑了你的帖子,但我括号的重点不是用“copy-assignment”替换“assignment”,而是用“operator”替换“constructor”...... (此外,“复制赋值运算符”通常简称为“赋值运算符”。“复制”前缀只是增加了精度:例如对于std::stringstring&amp; operator=(const string&amp;)string&amp; operator=(const char*) 都是赋值运算符,但只有第一个是一个 copy 赋值运算符,因为参数与类的类型相同。它与构造函数和 copy 构造函数是平行的。)

标签: c++ destructor copy-constructor assignment-operator


【解决方案1】:

对于您的复制赋值运算符,您可以使用copy-and-swap idiom

例如,在您的情况下,您可以将其添加到您的 IntList 类定义中(鉴于您的复制构造函数的代码,我假设您唯一的数据成员是 IntNode* first;):

        // --- swap non-member function ---
    friend void swap(IntList& a, IntList& b) /* no-fail */ {
        // swap data members one by one
        std::swap(a.first, b.first);
    }

        // --- (copy) assignment operator ---
    IntList& operator=(const IntList& other) {
        IntList temp(other); // copy construction
        swap(*this, temp);   // swap
        return *this;
                             // destruction of temp ("old *this")
    }

其实这可能是更好的风格:

        // --- swap non-member function ---
    friend void swap(IntList& a, IntList& b) /* no-fail */ {
        using std::swap; // enable ADL
        // swap data members one by one
        swap(a.first, b.first);
    }

        // --- (copy) assignment operator ---
    IntList& operator=(IntList other) { // note: take by value
        swap(*this, other);
        return *this;
    }

/* no-fail */ 注释可以在 C++98/03 中替换为 throw()(或者只是 /* throw() */ 注释),在 C++11 中可以替换为 noexcept

(注意:不要忘记预先包含std::swap的正确标题,即C++98/03中的&lt;algorithm&gt;,C++11中的&lt;utility&gt;(你也可以同时包含这两个).)

备注:这里swap 函数是在类主体中定义的,但它仍然是一个非成员 函数,因为它是一个friend

查看整个故事的链接帖子。

(当然,除了复制构造函数之外,您还必须提供正确的析构函数定义(参见What is The Rule of Three?)。)

【讨论】:

  • 请详细说明你的答案,而不是仅仅链接到不同的帖子。
  • @ThiefMaster 我已经用具体代码更新了我的答案。 (但链接的帖子仍然是必读的。)
【解决方案2】:

将您需要的功能放在由析构函数、复制构造函数和赋值运算符调用的单独函数中。

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多