将 shared_ptr 传递给函数 - 需要澄清

阿吉罗

我和会员一起上课

vector<shared_ptr<ParticleSystem>> particleSystems;

其中有一个方法

void AddParticleSystem(shared_ptr<ParticleSystem> const sys)
{
    particleSystems.push_back(sys);
}

注意参数。该方法的调用方式如下:

shared_ptr<ParticleSystem> psys = make_shared<ParticleSystem>(...);
scene.AddParticleSystem(psys);

它有效,但为什么呢?这不是共享吗?


默认情况下,我不会尝试传递指针。在考虑函数参数时,我要么使用const&when 希望不更改传递的变量上的任何内容,要么使用&when 计划使用将更改给定变量成员的方法。

所以默认情况下,我像这样实现了上述方法:

void AddParticleSystem(ParticleSystem const& sys)
{
    particleSystems.push_back(std::make_shared<ParticleSystem>(sys));
}

我称之为

shared_ptr<ParticleSystem> psys = make_shared<ParticleSystem>(...);
scene.AddParticleSystem(*psys);

这次它没有编译,说明

错误 C2280 'Physics::ParticleSystem::ParticleSystem(const Physics::ParticleSystem &)':试图引用已删除的函数

我使用Output(我使用 VS)追溯了这个问题,这导致我

particleSystems.push_back(std::make_shared<ParticleSystem>(sys));

make_shared的方法,要精确。

现在,这ParticleSystem扩展Visual了具有构造函数和成员的

Visual(string const &name, string const &path, const char* vertexPath, const char* fragmentPath, const char* geometryPath = nullptr, bool gamma = false)
{
    this->name = string().append(name);
    model = make_unique<Model>(path, gamma);
    material = make_unique<Material>();
    shader = make_unique<Shader>(vertexPath, fragmentPath, geometryPath);
}

unique_ptr<Shader> shader;
unique_ptr<Material> material;
unique_ptr<Model> model;
virtual ~Visual() = default;

make_shared需要以某种方式复制东西。问题ParticleSystem是 aVisualunique_ptr成员并且默认情况下make_shared不知道如何对待它们?

这是编译器删除我的默认复制构造函数的原因吗?如果是这样,如果我为所有具有的类Visual(包括它自己)实现一个复制构造函数,我可以将它ParticleSystem const&作为参数传递吗?

大卫·施瓦茨

a 的每个副本shared_ptr都是一个句柄,通过它可以访问共享对象。您可以传递shared_ptr对它或它的副本的引用,并将它的副本存储在集合中。shared_ptrs 的副本引用相同的底层对象。当最后shared_ptr一个引用相同底层对象的对象被销毁时,底层对象也被销毁。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过值将shared_ptr传递给lambda会导致内存泄漏

来自分类Dev

将“ this”称为shared_ptr?

来自分类Dev

从函数返回shared_ptr

来自分类Dev

将实例方法传递给需要C函数指针的API

来自分类Dev

将数组传递给方法时需要澄清

来自分类Dev

如何将值列表传递给需要数组的函数?

来自分类Dev

将shared_ptr传递给lambda时的C ++内存管理

来自分类Dev

将多维数组传递给** ptr函数

来自分类Dev

将shared_ptr传递给std :: function(成员函数)

来自分类Dev

如何将shared_ptr传递给寿命较短的类?

来自分类Dev

将shared_ptr传递给共享对象/ dll

来自分类Dev

将指针指向成员函数与std :: shared_ptr一起使用

来自分类Dev

将const shared_ptr <T>&传递给shared_ptr <T>作为参数

来自分类Dev

将lambda函数作为参数传递给需要接口的方法

来自分类Dev

在成员函数之间将shared_ptr用作私有变量C ++

来自分类Dev

如何将lambda传递给需要类参数的函数

来自分类Dev

如何将值传递给需要Class的函数

来自分类Dev

将double传递给需要uint8_t *的函数

来自分类Dev

将实例方法传递给需要C函数指针的API

来自分类Dev

将多维数组传递给** ptr函数

来自分类Dev

使用shared_ptr将原始指针传递给函数

来自分类Dev

将矩阵`shared_ptr <vector <vector <T <>>>>的行((* sp)[i])传递给接受`shared_ptr <vector <T >>`的函数

来自分类Dev

将shared_ptr传递给共享对象/ dll

来自分类Dev

将Ajax数据返回给调用函数-需要澄清先前的答案

来自分类Dev

在需要的值之前将默认值传递给函数

来自分类Dev

将lambda函数作为参数传递给需要接口的方法

来自分类Dev

将std :: shared_ptr传递给函数对象到std :: thread

来自分类Dev

将paramarray传递给需要paramarray的函数

来自分类Dev

需要澄清 - 将参数传递给函数 - Javascript

Related 相关文章

  1. 1

    通过值将shared_ptr传递给lambda会导致内存泄漏

  2. 2

    将“ this”称为shared_ptr?

  3. 3

    从函数返回shared_ptr

  4. 4

    将实例方法传递给需要C函数指针的API

  5. 5

    将数组传递给方法时需要澄清

  6. 6

    如何将值列表传递给需要数组的函数?

  7. 7

    将shared_ptr传递给lambda时的C ++内存管理

  8. 8

    将多维数组传递给** ptr函数

  9. 9

    将shared_ptr传递给std :: function(成员函数)

  10. 10

    如何将shared_ptr传递给寿命较短的类?

  11. 11

    将shared_ptr传递给共享对象/ dll

  12. 12

    将指针指向成员函数与std :: shared_ptr一起使用

  13. 13

    将const shared_ptr <T>&传递给shared_ptr <T>作为参数

  14. 14

    将lambda函数作为参数传递给需要接口的方法

  15. 15

    在成员函数之间将shared_ptr用作私有变量C ++

  16. 16

    如何将lambda传递给需要类参数的函数

  17. 17

    如何将值传递给需要Class的函数

  18. 18

    将double传递给需要uint8_t *的函数

  19. 19

    将实例方法传递给需要C函数指针的API

  20. 20

    将多维数组传递给** ptr函数

  21. 21

    使用shared_ptr将原始指针传递给函数

  22. 22

    将矩阵`shared_ptr <vector <vector <T <>>>>的行((* sp)[i])传递给接受`shared_ptr <vector <T >>`的函数

  23. 23

    将shared_ptr传递给共享对象/ dll

  24. 24

    将Ajax数据返回给调用函数-需要澄清先前的答案

  25. 25

    在需要的值之前将默认值传递给函数

  26. 26

    将lambda函数作为参数传递给需要接口的方法

  27. 27

    将std :: shared_ptr传递给函数对象到std :: thread

  28. 28

    将paramarray传递给需要paramarray的函数

  29. 29

    需要澄清 - 将参数传递给函数 - Javascript

热门标签

归档