使用公共方法交换私有唯一指针

huangapple go评论76阅读模式
英文:

Swapping private unique pointers with a public method

问题

我有一个类,其中有一个私有的 unique_ptr 成员,我想要暴露一个公共的 swap 函数,用来交换它的 unique_ptr 成员的所有权和提供的其他对象的 unique_ptr 成员的所有权。我该如何做?

```cpp
class A { // A 是多态类型,派生自 A1、A2 等
    private:
        std::unique_ptr<B> b;
    public:
        void swap_b(A& other) {
            std::swap(b, other.b); // 我想要交换 A.b 和 other.b
        }
};

class B { // B 是多态类型
    private:
        std::unique_ptr<C> c; // c 也是多态类型
    public:
        void swap_C(B& other) {
            std::swap(c, other.c);
        }
};

int main() {
    std::unique_ptr<A> alphaOne = std::make_unique<A1>(...);
    std::unique_ptr<A> alphaTwo = std::make_unique<A2>(...);
    alphaOne->swap_b(*alphaTwo); // unique_ptr alphaOne->b->c 应该与 alphaTwo->b->c 交换所有权
}

我有一个 vector<std::unique_ptr<A>>,我想从中获取任意两个元素并调用 swap 函数来交换它们的 b.c unique_ptr。我该如何实现这个?

std::vector<std::unique_ptr<A>> myVector;

// 假设 myVector 至少包含两个元素
size_t index1 = 0; // 第一个元素的索引
size_t index2 = 1; // 第二个元素的索引

myVector[index1]->swap_b(*myVector[index2]); // 交换两个元素的 unique_ptr A->b->c
英文:

I have a class with a private unique_ptr member and I want to expose a public swap function which will swap ownership of its unique_ptr member with that of the provided other. How can I do so?

class A{ // a is polymorphic type, derivatives A1, A2, etc
    private:
        unique_ptr&lt;B&gt; b;
    public:
        void swap_b(A??? other) {
            A.b.swap_c(other.b) // I want to swap A.b.c with other.b.c
        }
};

class B{ // b is polymorphic type
    private:
        unique_ptr&lt;C&gt; c; // c is also polymorphic type
    public:
        void swap_C(B??? other) {
            B.c.swap(other.c)
        }
};

int main() {
    unique_ptr&lt;A&gt; alphaOne = make_unique&lt;A1&gt;(...);
    unique_ptr&lt;A&gt; alphaTwo = make_unique&lt;A2&gt;(...);
    alphaOne.swap(alphaTwo); // the unique_ptr alphaOne.b.c should swap ownership with alphaTwo.b.c
}

I have a vector&lt;unique_ptr&lt;A&gt;&gt;, from which I want to grab any two elements and call swap on them to swap their b.c unique_ptrs. How can I achieve this?

答案1

得分: 2

  1. 基类的析构函数在通过基类指针销毁实例时应该是 virtual 的。
英文:

I am unsure if I am missing something, but a simple reference should suffice:

#include &lt;memory&gt;
using namespace std;

class C {};

class B {
   private:
    unique_ptr&lt;C&gt; c;

   public:
    void swap(B&amp; other){ c.swap(other.c); }
};

class A {
   private:
    unique_ptr&lt;B&gt; b;

   public:
    virtual ~A() = default; // See note 1.

    void swap(A&amp; other){ b.swap(other.b); }
};

class A1:public A{};
class A2:public A{};


int main() {
    unique_ptr&lt;A&gt; alphaOne = make_unique&lt;A1&gt;();
    unique_ptr&lt;A&gt; alphaTwo = make_unique&lt;A2&gt;();
    alphaOne.swap(alphaTwo);
}
  1. The destructor of the base class should be virtual when you destroy instances via a base class pointer.

huangapple
  • 本文由 发表于 2023年7月23日 15:53:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定