By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

how to have forward declaration from different namespace?

P: n/a
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

Sep 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

toton wrote:
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
Got the answer!
I also need to write
using ui::InkFrame or fully quilified name inside the class as type,
along with forward declaration like
namespace ui{
class InkFrame;
}
Thanks to the newsgroup ...

Sep 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.