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.
Code:
class CMLIB_EXPORT MasterController
: public QObject{
Q_OBJECT
public:
explicit MasterController
(QObject* parent
= nullptr
);
~MasterController();
cm::controllers::NavigationController* ui_navigationController();
signals:
private:
class Implemantation;
QScopedPointer<Implemantation> implementation;
};
*** 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.
Code:
class MasterController::Implemantation
{
public:
Implemantation(MasterController* _masterController) : masterController(_masterController)
{
navigationController = new NavigationController(masterController);
}
~Implemantation()
{
}
MasterController* masterController;
NavigationController* navigationController;
QString welcomeMessage
= "This is MasterController to Major Tom";
};
{
//implementation is a scoped pointer
implementation.reset(new Implemantation(this));
}
MasterController::~MasterController()
{
}
NavigationController* MasterController::ui_navigationController()
{
return implementation->navigationController;
}
*** 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 *)'
with
[
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
Re: QScopedPointer usage with Forward Declaration
From the Qt documentation
Quote:
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.