Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSharedData and the rule of five
Forum Updated to NodeBB v4.3 + New Features

QSharedData and the rule of five

Scheduled Pinned Locked Moved Unsolved General and Desktop
rule of fiveqshareddatamove ctormove assignmentqshareddataptr
1 Posts 1 Posters 201 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clint Westwood
    wrote on last edited by
    #1

    In my class that uses QSharedData and QSharedDataPointer I have to explicitly define the destructor, copy constructor and copy assignment operator in the cpp file. Simply using the default 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 of QSharedData with QSharedDataPointer, even with just the default 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;
    }
    
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved