Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I write the move assignment function for this derived class?
    text
    copied!<p><a href="https://connect.microsoft.com/VisualStudio/feedback/details/800114/-default-not-working-for-move-constructor-in-visual-c-2013-rc">Due to this bug in Visual Studio 2013</a>, 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.</p> <p>Here's the code:</p> <pre><code>#include &lt;utility&gt; // Base class; movable, non-copyable class shader { public: virtual ~shader() { if (id_ != INVALID_SHADER_ID) { // Clean up } } // Move assignment shader&amp; operator=(shader&amp;&amp; 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&amp;&amp; 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&amp;) = delete; shader&amp; operator=(const shader&amp;) = 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&amp; operator=(vertex_shader&amp;&amp;) = default; // ... so I have to write my own. vertex_shader&amp; operator=(vertex_shader&amp;&amp;) { // What goes here? return *this; } vertex_shader(vertex_shader&amp;&amp; other ) { *this = std::move(other); } private: // vertex_shader is non-copyable vertex_shader(const vertex_shader&amp;) = delete; vertex_shader&amp; operator=(const vertex_shader&amp;) = delete; }; int main(int argc, char* argv[]) { vertex_shader v; // later on v = vertex_shader{ "vertex_shader.glsl" }; return 0; } </code></pre> <p>What should the move assignment function in the derived class look like?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload