我如何写这个派生类的移动分配函数? [英] How do I write the move assignment function for this derived class?

查看:244
本文介绍了我如何写这个派生类的移动分配函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Visual Studio 2013中的此错误,我需要为派生类提供自己的移动构造函数和移动分配。但是,我不知道如何为基类调用适当的移动函数。

Due to this bug in Visual Studio 2013, I need to provide my own move constructor and move assignment for a derived class. However, I don't know how to call the appropriate move functions for the base class.

这里是代码:

#include <utility>

// Base class; movable, non-copyable 
class shader
{
    public:
        virtual ~shader()
        {
            if (id_ != INVALID_SHADER_ID)
            {
                // Clean up
            }
        }

        // Move assignment
        shader& operator=(shader&& other)
        {
            // Brett Hale's comment below pointed out a resource leak here.
            // Original:
            // id_ = other.id_;
            // other.id_ = INVALID_SHADER_ID;
            // Fixed:
            std::swap( id_, other.id_ );
            return *this;
        }

        // Move constructor
        shader(shader&& other)
        {
            *this = std::move(other);
        }

    protected:
        // Construct an invalid shader.
        shader()
            : id_{INVALID_SHADER_ID}
        {}

        // Construct a valid shader
        shader( const char* path )
        {
            id_ = 1;
        }

    private:
        // shader is non-copyable
        shader(const shader&) = delete;
        shader& operator=(const shader&) = delete;

        static const int INVALID_SHADER_ID = 0;

        int id_;
        // ...other member variables.
};

// Derived class
class vertex_shader final : public shader
{
    public:
        // Construct an invalid vertex shader.
        vertex_shader()
            : shader{}
        {}

        vertex_shader( const char* path )
            : shader{path}
        {}

        // The following line works in g++, but not Visual Studio 2013 (see link at top)...
        //vertex_shader& operator=(vertex_shader&&) = default;

        // ... so I have to write my own.
        vertex_shader& operator=(vertex_shader&&)
        {
            // What goes here?
            return *this;
        }

        vertex_shader(vertex_shader&& other )
        {
            *this = std::move(other);
        }

    private:
        // vertex_shader is non-copyable
        vertex_shader(const vertex_shader&) = delete;
        vertex_shader& operator=(const vertex_shader&) = delete;
};

int main(int argc, char* argv[])
{
    vertex_shader v;

    // later on
    v = vertex_shader{ "vertex_shader.glsl" };

    return 0;
}

派生类中的移动赋值函数应该是什么样子?

What should the move assignment function in the derived class look like?

推荐答案

您只需要调用基类move赋值运算符:

You just need to call the base class move assignment operator:

    vertex_shader& operator=(vertex_shader&& rhs)
    {
        shader::operator=(std::move(rhs));
        return *this;
    }

这篇关于我如何写这个派生类的移动分配函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆