QSharedData and the rule of five
-
In my class that uses
QSharedData
andQSharedDataPointer
I have to explicitly define the destructor, copy constructor and copy assignment operator in the cpp file. Simply using thedefault
keyword is enough. This, however, prevents the implicit definition of the move constructor and move assignment operator.So should the same be done for these moving member functions as well, that is definition in a cpp file with the
default
keyword, especially in the context of preserving the ABI stability (the code in question goes to a DLL)? Is it the correct practice to explicitly define all special member functions when using a combination ofQSharedData
withQSharedDataPointer
, even with just thedefault
keyword?An example:
TestClass.h:
#pragma once #include <QSharedDataPointer> class Test { public: Test (); // Defined to prevent this MSVC error: C2027: use of undefined type 'Test::Implementation' ~Test (); Test (const Test& other); Test& operator= (const Test& other); // Add this? // Test (Test&& other); // Test& operator= (Test&& other); private: struct Implementation; QSharedDataPointer <Implementation> d; };
TestClass.cpp:
#include <QSharedData> #include "TestClass.h" struct Test::Implementation : public QSharedData { QVector <Test> vector; }; Test::Test () : d (new Implementation) {} Test::~Test () = default; Test::Test (const Test& other) = default; Test& Test::operator= (const Test& other) = default; // Add this? // Test::Test (Test&& other) = default; // Test& Test::operator= (Test&& other) = default;
Main.cpp:
#include "TestClass.h" int main (int, char* []) { Test first; // Will not compile without Test::~Test () = default; Test second (first); // Will not compile without Test::Test (const Test& other) = default; second = first; // Will not compile without Test& Test::operator= (const Test& other) = default; return 0; }