C++ 如何从具有移动语义的类对象生成共享指针

C++ 如何从具有移动语义的类对象生成共享指针,c++,C++,这是我的网格对象类 struct texture { std::string textureName; unsigned int textureId; }; class Mesh { public: /* Mesh data */ std::vector<texture> textures; /* Functions */ Mesh(std::vector<texture> &text); Mes

这是我的网格对象类

struct texture
{
    std::string textureName;
    unsigned int textureId;
};

class Mesh
{
public:
    /* Mesh data */
    std::vector<texture> textures;
    
    /* Functions */
    Mesh(std::vector<texture> &text);
    Mesh(const Mesh& mesh) = delete;
    Mesh& operator = (const Mesh& mesh) = delete;
    Mesh(Mesh &&other);
    Mesh& operator=(Mesh &&other);
    ~Mesh();        

};




 #include "Mesh.h"

Mesh::Mesh(std::vector<texture> &text) : textures(text)
{
}

Mesh::Mesh(Mesh &&other) : textures( other.textures)
{
    for (auto &tex : other.textures)
        tex.textureId = 0;
}

Mesh& Mesh::operator=(Mesh &&other)
{
    if (this != &other)
    {
        textures = other.textures;
    }

    for (auto &tex : other.textures)
        tex.textureId = 0;

    return *this;
}

Mesh::~Mesh()
{
    for (auto &tex : textures)
        glDeleteTextures(1, &tex.id);
}
现在我意识到我需要为这个类对象创建一个共享指针,但是当我尝试这样做时,尝试引用一个已删除的函数时会出现错误,我相信这是因为我使用了移动语义

std::vector< texture> textures;
    Mesh m(textures);
    std::shared_ptr<Mesh> myMesh = std::make_shared<Mesh>(m);
是否可以使用移动语义从类对象创建共享指针

错误消息


错误C2280“Mesh::Meshconst Mesh&”:尝试引用已删除的函数

如果添加移动构造函数,则默认复制构造函数将被隐式删除,并且std::make_sharedm使用复制构造函数。这就是您收到如下错误消息的原因:

error: use of deleted function 'constexpr Mesh::Mesh(const Mesh&)'
            -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))

note: 'constexpr Mesh::Mesh(const Mesh&)' is implicitly declared as deleted 
          because 'Mesh' declares a move constructor or move assignment operator

但是对于所显示的代码,根本不清楚您为什么要执行移动,因为您似乎没有将m用于任何其他内容。

如果添加移动构造函数,则默认的复制构造函数将被隐式删除,并且std::make\u sharedm使用复制构造函数。这就是您收到如下错误消息的原因:

error: use of deleted function 'constexpr Mesh::Mesh(const Mesh&)'
            -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))

note: 'constexpr Mesh::Mesh(const Mesh&)' is implicitly declared as deleted 
          because 'Mesh' declares a move constructor or move assignment operator
但是对于所显示的代码,您根本不清楚为什么要移动,因为您似乎没有将m用于其他任何东西。

make\u shared使用参数来构造网格。你要么想要

std::shared_ptr<Mesh> myMesh2 = std::make_shared<Mesh>(std::move(m));

从两个未删除的构造函数中选择一个。

make\u shared使用参数来构造网格。你要么想要

std::shared_ptr<Mesh> myMesh2 = std::make_shared<Mesh>(std::move(m));


从两个未删除的构造函数中选择一个。

最好在问题中包含错误消息。您已经得到了答案,但您仍然可以添加it@idclev463035818添加了错误消息。在问题中包含错误消息会很好。您已经得到了答案,但您仍然可以添加it@idclev463035818添加了错误消息。我简化了类只是为了显示问题所在,这是一个opengl网格对象,在析构函数中它删除了所有纹理id。@Summit这是一个opengl网格对象,在析构函数中它删除了所有纹理id,这听起来有点像是你在概念上的误解。网格的移动构造函数应确保从中移动的网格对象不会管理与移动到的对象相同的对象。m和myMesh管理的对象是两个不同的实例,两个实例都将调用析构函数。这正是我想要的,在树视图中,当有人单击对象时,它调用复制构造函数并向前传递该对象,当调用该对象的析构函数时,我不希望它从图形内存中删除textureId。我简化了类,只是为了说明问题所在,这是一个opengl网格对象,在析构函数中它删除了所有的纹理id。@Summit这是一个opengl网格对象,在析构函数中它删除了所有的纹理id,这听起来有点像一个概念上的误解。网格的移动构造函数应确保从中移动的网格对象不会管理与移动到的对象相同的对象。m和myMesh管理的对象是两个不同的实例,两个实例都将调用析构函数。这正是我想要的,在树视图中,当有人单击对象时,它调用复制构造函数并向前传递该对象,当调用该对象的析构函数时,我不希望它从图形内存中删除textureId。