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

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

Swapping private unique pointers with a public method

问题

  1. 我有一个类,其中有一个私有的 unique_ptr 成员,我想要暴露一个公共的 swap 函数,用来交换它的 unique_ptr 成员的所有权和提供的其他对象的 unique_ptr 成员的所有权。我该如何做?
  2. ```cpp
  3. class A { // A 是多态类型,派生自 A1、A2 等
  4. private:
  5. std::unique_ptr<B> b;
  6. public:
  7. void swap_b(A& other) {
  8. std::swap(b, other.b); // 我想要交换 A.b 和 other.b
  9. }
  10. };
  11. class B { // B 是多态类型
  12. private:
  13. std::unique_ptr<C> c; // c 也是多态类型
  14. public:
  15. void swap_C(B& other) {
  16. std::swap(c, other.c);
  17. }
  18. };
  19. int main() {
  20. std::unique_ptr<A> alphaOne = std::make_unique<A1>(...);
  21. std::unique_ptr<A> alphaTwo = std::make_unique<A2>(...);
  22. alphaOne->swap_b(*alphaTwo); // unique_ptr alphaOne->b->c 应该与 alphaTwo->b->c 交换所有权
  23. }

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

  1. std::vector<std::unique_ptr<A>> myVector;
  2. // 假设 myVector 至少包含两个元素
  3. size_t index1 = 0; // 第一个元素的索引
  4. size_t index2 = 1; // 第二个元素的索引
  5. 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?

  1. class A{ // a is polymorphic type, derivatives A1, A2, etc
  2. private:
  3. unique_ptr&lt;B&gt; b;
  4. public:
  5. void swap_b(A??? other) {
  6. A.b.swap_c(other.b) // I want to swap A.b.c with other.b.c
  7. }
  8. };
  9. class B{ // b is polymorphic type
  10. private:
  11. unique_ptr&lt;C&gt; c; // c is also polymorphic type
  12. public:
  13. void swap_C(B??? other) {
  14. B.c.swap(other.c)
  15. }
  16. };
  17. int main() {
  18. unique_ptr&lt;A&gt; alphaOne = make_unique&lt;A1&gt;(...);
  19. unique_ptr&lt;A&gt; alphaTwo = make_unique&lt;A2&gt;(...);
  20. alphaOne.swap(alphaTwo); // the unique_ptr alphaOne.b.c should swap ownership with alphaTwo.b.c
  21. }

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:

  1. #include &lt;memory&gt;
  2. using namespace std;
  3. class C {};
  4. class B {
  5. private:
  6. unique_ptr&lt;C&gt; c;
  7. public:
  8. void swap(B&amp; other){ c.swap(other.c); }
  9. };
  10. class A {
  11. private:
  12. unique_ptr&lt;B&gt; b;
  13. public:
  14. virtual ~A() = default; // See note 1.
  15. void swap(A&amp; other){ b.swap(other.b); }
  16. };
  17. class A1:public A{};
  18. class A2:public A{};
  19. int main() {
  20. unique_ptr&lt;A&gt; alphaOne = make_unique&lt;A1&gt;();
  21. unique_ptr&lt;A&gt; alphaTwo = make_unique&lt;A2&gt;();
  22. alphaOne.swap(alphaTwo);
  23. }
  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:

确定