PDA

View Full Version : Migrating content from header file to source file



ehnuh
3rd October 2012, 03:17
Hi,I am trying out an implementation for my header and source files with a user interface.
The default for Qt is that it generates the form below for the header and source files when using the wizard.
For example I wanted to create a class using QWidget as its base class.

the header file is:

#include <QWidget>
#include <QCloseEvent>
#include <QMessageBox>

namespace Ui{

class frmBitMainPanel;
}

class BitMainPanel : public QWidget
{

Q_OBJECT

public:
explicit BitMainPanel(QWidget *ParentPtr = 0);


private:

Ui::frmBitMainPanel *UiPanelPtr;

protected:
void closeEvent(QCloseEvent *);


the source file is:

BitMainPanel::BitMainPanel(QWidget *ParentPtr) : QWidget(ParentPtr), UiPanelPtr(new Ui::frmBitMainPanel)
{

UiPanelPtr->setupUI(this);
}

--question is, if I were to implement my headers to only contain function prototypes and the source files to contain the declarations such as the Ui::frmBitMainPanel *UiPanelPtr how would I go on about this?

I am going to migrate Ui::frmBitMainPanel *UiPanelPtr to the source file. Same goes with all future declarations. Is it possible?

ChrisW67
3rd October 2012, 04:36
Not in the sense I think you are expecting: there will be some private data in almost every useful class.

However, the private implementation idiom (AKA pimpl or handle pattern) will get you much of the way there. You have a forward declaration and a single pointer to a private class in the public header and everything else about the private class is, well, private and used only from the CPP file. This maximises binary compatibility by minimising the exposed implementation details... which, I assume, is the unstated reason for wanting to do this.

wysota
3rd October 2012, 17:00
If I can add to what Chris already said -- the schema posted in this thread already uses the private implementation idiom -- note, that Ui::frmBitMainPanel is only forward declared in the header file and all its implementation details are hidden by including ui_xxxx.h in the cpp file only. Units using the header file are only informed that "somewhere there may exist a class called Ui::frmBitMainPanel".