【问题标题】:static std::shared_ptr<T> member; vs std::shared_ptr<T> member;?静态 std::shared_ptr<T> 成员; vs std::shared_ptr<T> 成员;?
【发布时间】:2020-05-10 14:54:39
【问题描述】:

现在,我想,我必须在我的计划中做出一个重要的决定。任务如下:

我有一个class base,它将被多个派生类继承。派生类必须对base 成员的相同实例具有共享访问权限。另外,我想从外部访问那些“共享”Bases 成员:从包含所有派生类的第三个class Foo。根据我目前的经验,我想出了两个解决方案:

  • 第一个是使用共享指针。使用structs、ints 并将所有内容放在构造函数中以简化操作。
struct Base
{
  std::shared_ptr<int> m_1 = nullptr;
  std::shared_ptr<int> m_2 = nullptr;
};

struct Derived_1 : Base
{
  // using Base members and doing other stuff
};

struct Derived_2 : Base
{
  // using Base members and doing other stuff
};

struct Foo
{
  std::shared_ptr<int> m_1 = std::make_shared<int>();
  std::shared_ptr<int> m_2 = std::make_shared<int>();

  Derived_1 _d1;
  Derived_2 _d2;

  Foo()
  {
    _d1.m_1 = this->m_1;
    _d1.m_2 = this->m_2;

    _d2.m_1 = this->m_1;
    _d2.m_2 = this->m_2;
  }
};

我可以从Foo 和派生类访问相同的m_1 和m_2。

  • 第二种解决方案涉及使用static关键字:
struct Base
{
  static std::shared_ptr<int> m_1;
  static std::shared_ptr<int> m_2;
};

// providing definitions for Base::m_1 and Base::m_2 ...

// Derived classes stay the same as in first example.

class Foo
{
  std::shared_ptr<int> m_1;
  std::shared_ptr<int> m_2;

  Foo()
  {
    this->m_1 = Base::m_1;
    this->m_2 = Base::m_2;
  }

};

在这两种情况下,我都取得了相同的结果(真的吗?)。一种方法比另一种方法有什么优势吗?他们有什么隐藏的缺点?什么是更常见的方法,为什么?我对其他方法很好奇。我在VS17下使用的是c++17。

【问题讨论】:

  • 在这两种情况下都打印sizeof(Base),这可能很有启发性。使用第一种方法,您会在Base 的每个实例中存储相同数据的副本,从而浪费空间。
  • 不清楚为什么你甚至有Base 类,它似乎没有任何用处。你可能想要一个单例来存放共享数据。
  • Base 本身并不多,它只拥有那些共享成员,但共享成员是那些完成所有工作的人。派生类必须能够访问来自 Base 成员的某些计算结果。我同意单例也可以是一个解决方案。
  • 你真正想要实现什么?如果所有Foos 都需要访问公共int,则使其成为Foo 的静态成员。我已经写了一个答案,但我仍然不明白这一切有什么好处。也许xy problem
  • 顺便说一句,您的标题要求static T vs shared_ptr&lt;T&gt;,但您的代码是static shared_ptr&lt;T&gt; vs shared_ptr&lt;T&gt;

标签: c++ inheritance static shared-ptr


【解决方案1】:

在这两种情况下,我都取得了相同的结果(真的吗?)。

嗯,不。如果使用静态成员,则该成员不与任何实例关联,并且程序中分别只有一个共享Base::m_1和一个Base::m_2。对于非静态成员,Base 的每个实例都包含自己的 m_1 和 m_2。

一种方法比另一种方法有什么优势吗?

一个的优点是程序中只有一个成员,另一个的优点是每个实例都有一个成员。

什么是更常见的方法,为什么?

非静态成员通常更有用。静态存储是全局状态,存在问题,不鼓励使用。


目前尚不清楚为什么Foo 具有成员m_1 和m2,它们似乎指向与成员指向的对象相同的对象。为什么不简单地使用这些指针并摆脱 Foo 中的指针?

struct Foo
{
  Derived_1 _d1{std::make_shared<int>(), std::make_shared<int>()};
  Derived_2 _d2{_d1.m_1,                 _d1.m_2};
};

共享派生对象的基类也是可能的,这将实现每个Foo 实例只有一个共享指针副本。但只有当派生实例本身是单个派生类的基础时才有可能。在这种情况下,基类的共享是通过虚拟继承来实现的。

struct Derived_1 : virtual Base {};
struct Derived_2 : virtual Base {};
struct Derived   : Derived_1, Derived_2 {
    Derived(std::shared_ptr<int> m_1, std::shared_ptr<int> m_2): Base{m_1, m_2} {}
};

struct Foo
{
  Derived _d{std::make_shared<int>(), std::make_shared<int>()};
};

【讨论】:

  • 至“为什么不在基址中使用指针”:因为正如我所见,Foo 并不意味着继承自Base,它“不是基址”,而是“它具有(来自) 基类(派生类)”。将Foo : public Base 与static 内的Base 成员一起使用会起作用,但这不是它的本意。
  • @user12700880 我读错了。我已经编辑了句子以匹配相关代码。
  • 我在Foo 中有m_1 和m_2,因为我想从Foo 访问它们,而不仅仅是从派生类。在static-case 中,我不想总是做Base::m_1,所以我在Foo 中保存了一个副本。在另一种情况下,m_1 和 m_2 归 Foo 所有并共享给派生类,所以我也想要访问。
  • 你的代码被剪断了,但我可以通过Foo 访问m_1 内部_d1.m_1 或_d2.m_1
【解决方案2】:

它们不一样。考虑一下:

int main() {
    Foo f1;
    Foo f2;
    *(f1.m_1) = 5;
    *(f2.m_1) = 42;
    std::cout << *(f1.m_1);
}

对于没有static 的第一个版本,它将输出:5,因为每个Foo 都使用自己的Base 子对象。第二个变种有一些错误:

#include <memory>
#include <iostream>
struct Base
{
  static std::shared_ptr<int> m_1;
  static std::shared_ptr<int> m_2;
};

// defintions were missing
std::shared_ptr<int> Base::m_1 = std::make_shared<int>();
std::shared_ptr<int> Base::m_2 = std::make_shared<int>();

// Foo didnt inherit from Base and had a private constructor
struct Foo : Base
{
  std::shared_ptr<int> m_1;
  std::shared_ptr<int> m_2;

  Foo()
  {
    this->m_1 = Base::m_1;
    this->m_2 = Base::m_2;
  }
};

修复后,上面的main 打印42,因为只有一个Base::m_1,只有一个Base::m_2。

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多