I have a class that has some data members that I want to be hidden from the caller (because including the headers for their types significantly increases the compile time, and it would require every project using this class to add an additional path to their include paths).
This class uses QSharedDataPointer
to store this data. This way it can be copied by using the default copy constructor.
The basic structure of this class is:
class MyClass {
private:
QSharedDataPointer<MySharedClassData> m_data;
};
Is there any fancy trick to do this without defining MySharedClassData
(which inherits from QSharedData
) in the same header file? Or is there any other good way of hiding data fields?
I've already tried a forward declaration of MySharedClassData
but this didn't work (despite the fact that m_data
is private
).
The only solution I can currently thing of is to declare m_data
as QSharedDataPointer<QSharedData>
but then I need to cast the data member every time I want to access it. Is there a better solution?
The forward declaration should be working as long as you constructor and destructor are not defined in the header. The following class compiles on my computer:
#ifndef MAIN_WINDOW_HXX
#define MAIN_WINDOW_HXX
#include <QMainWindow>
#include <ui_MainWindow.h>
#include <QSharedDataPointer>
class MySharedClassData;
class MainWindow : public QMainWindow, private Ui_MainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
virtual ~MainWindow();
QSharedDataPointer<MySharedClassData> m_data;
};
#endif
If you try to inline your constructor/destructor, then you might receive a: C2027: use of undefined type 'type'
under VS.