Hi,
I have two namespace contains class InkFrame and PrefDialog
respectively. PrefDialog needs InkFrame to show the dialog over the
frame. It also stores a pointer to InkFrame inside it.
Now I want InkFrame to be forward declared in the PrefDialog header
file rather than to be included. I want to include it in the cpp file
instead. There is no harm including it in PrefDialog header, and it
works. But I want to save some compilation time. That is why I thought
to forward declare it (and all the other classes whenever possible)
instead of include. They works perfectly if both of them are from same
namespace. But fails to work if they are from different namespace.
To make it little clear, the headers are
InkFrame.hpp =>
namespace ui{
class InkFrame : public Frame{
...
} ;
}
here Frame, is a library class and necessary headers are already there.
PrefDialog.hpp =>
#include "InkFrame.hpp" //and other necessary headers.
using ui::InkFrame
namespace client{
namespace ui{
class PrefDialog : public Dialog{
InkFrame* _parent;
public:
PrefDialog(InkFrame* parent);
};
}
}
This code works.
Next I removed include & have forward declaration. The include and
using statement is shifted to cpp file for PrefDialog.
PrefDialog.hpp =>
//#include "InkFrame.hpp" //and other necessary headers.
//using ui::InkFrame
namespace client{
namespace ui{
class InkFrame; //forward declaration.
class PrefDialog : public Dialog{
InkFrame* _parent;
public:
PrefDialog(InkFrame* parent);
};
}
}
Here, the code assumes InkFrame is from client::ui (which is quite
reasonable, I think) and fails to compile the code as cpp gets it from
ui namespace.
declaring at top class ui::InkFrame also do not work, as the ui
namespace is not defined here!
also declaring like
namespace ui{
class InkFrame;
}
at the top is not working. These two cases the header do not see the
declaration even, while in the firest case it see and find a mismatch
at the cpp.
Thus, what is the common way to forward declare a class from a
different namespace?
Thanks for any advice.
abir