Results 1 to 2 of 2

Thread: QScopedPointer usage with Forward Declaration

  1. #1
    Join Date
    Feb 2021
    Qt products

    Default QScopedPointer usage with Forward Declaration

    Hello everyone, this will be my very first thread. I am currently learning qt from tutorials and books. At some point, I implemented this class with Private
    Implementation covered with QScopedPointer.

    ***1) Here is my MasterController class with a forward declaration of class "Implementation" with QScoped Pointer guards it.

    Qt Code:
    1. class CMLIB_EXPORT MasterController : public QObject
    2. {
    3. Q_OBJECT
    5. public:
    6. explicit MasterController(QObject* parent = nullptr);
    7. ~MasterController();
    9. cm::controllers::NavigationController* ui_navigationController();
    11. signals:
    12. private:
    14. class Implemantation;
    15. QScopedPointer<Implemantation> implementation;
    16. };
    To copy to clipboard, switch view to plain text mode 

    *** 2) Here is also my cpp file for MasterController class. Here we see previously declared Implemation class as well as constructor, deconstructor of Master Class.

    Qt Code:
    1. class MasterController::Implemantation
    2. {
    3. public:
    4. Implemantation(MasterController* _masterController) : masterController(_masterController)
    5. {
    6. navigationController = new NavigationController(masterController);
    7. }
    9. ~Implemantation()
    10. {
    11. }
    13. MasterController* masterController;
    14. NavigationController* navigationController;
    15. QString welcomeMessage = "This is MasterController to Major Tom";
    16. };
    19. MasterController::MasterController(QObject* parent) : QObject(parent)
    20. {
    21. //implementation is a scoped pointer
    22. implementation.reset(new Implemantation(this));
    23. }
    25. MasterController::~MasterController()
    26. {
    27. }
    29. NavigationController* MasterController::ui_navigationController()
    30. {
    31. return implementation->navigationController;
    32. }
    To copy to clipboard, switch view to plain text mode 

    *** 3) Here is my Question: My private implemented Class (Implementation) has 3 members :
    a- MasterController class that it lives on
    b- Someother Navigation Class
    c- A String

    If my MasterController class comes to the end of scope , everythis is fine, all objects are deleted. From curiosity, i deleted Destructor from MasterController and i got following compile errors:

    C:\Qt\5.14.2\msvc2017_64\include\QtCore\qscopedpoi nter.h:57: error: C2027: use of undefined type 'cm::controllers::MasterController::Implemantation '
    c:\qtprojects\cm-learningqt\customermanager\cm-lib\source\controllers\mastercontroller.h:34: see declaration of 'cm::controllers::MasterController::Implemantation '
    C:\Qt\5.14.2\msvc2017_64\include\QtCore\qscopedpoi nter.h:53: while compiling class template member function 'void QScopedPointerDeleter<T>::cleanup(T *)'
    T=cm::controllers::MasterController::Implemantatio n

    I would be pleased if someone explains what the problem is and why destructor is important here.
    Thanks in advance

  2. #2
    Join Date
    Dec 2008
    Thanked 2 Times in 1 Post

    Default Re: QScopedPointer usage with Forward Declaration

    From the Qt documentation

    Classes that are forward declared can be used within QScopedPointer, as long as the destructor of the forward declared class is available whenever a QScopedPointer needs to clean up.

    Concretely, this means that all classes containing a QScopedPointer that points to a forward declared class must have non-inline constructors, destructors and assignment operators
    I believe the issue is the compiler will implicitly define a default constructor in places where your type is still incomplete causing the error.

  3. The following 2 users say thank you to peppermg for this useful post:

    ca (12th February 2021), d_stranz (11th February 2021)

Similar Threads

  1. Forward declaration fails
    By [?]jNs in forum Newbie
    Replies: 5
    Last Post: 27th July 2011, 12:28
  2. Forward Declaration of struct QCloseEvent ????
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 3rd February 2009, 16:16
  3. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 22:45
  4. Forward declaration with std namespace
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 5th March 2007, 21:45
  5. Why forward declaration ?
    By probine in forum General Programming
    Replies: 3
    Last Post: 26th January 2007, 19:22


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.