Forward declaration placement ?
-
I have (some basic ) understanding of purpose of "forward declaration". I am using the orignal ( 3rd party ) code and wonder what would be the purpose of "enclosing forward declaration" in "namespace". My test, declared class is yet not defined ,compiles and runs , regardless where I place the forward declaration.
#include ....
class MdiChild;
class terminal_ORIGINAL_MainWindow;QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QMdiArea;
class QMdiSubWindow;// add Apr3
// class terminal_ORIGINAL_MainWindow;QT_END_NAMESPACE
// case 40:class MainWindow : public QMainWindow
{
Q_OBJECT....
-
I have (some basic ) understanding of purpose of "forward declaration". I am using the orignal ( 3rd party ) code and wonder what would be the purpose of "enclosing forward declaration" in "namespace". My test, declared class is yet not defined ,compiles and runs , regardless where I place the forward declaration.
#include ....
class MdiChild;
class terminal_ORIGINAL_MainWindow;QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QMdiArea;
class QMdiSubWindow;// add Apr3
// class terminal_ORIGINAL_MainWindow;QT_END_NAMESPACE
// case 40:class MainWindow : public QMainWindow
{
Q_OBJECT....
If you put a forward declaration inside a namespace, it is then known within that namespace. If your code compiles regardless of whether you put a forward declaration inside a namespace, it could be that you're actually creating two versions of the symbol: one inside the namespace, and one in the global namespace. But you haven't provided enough code for me to tell. A forward declaration is used to avoid an
#include
for a symbol that is used in name only, which means that it is only used in a reference or pointer (name&
orname*
). In such a case, the compiler doesn't need to know the size of the class or any of its members; it only needs to know that the symbol exists.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
If you put a forward declaration inside a namespace, it is then known within that namespace. If your code compiles regardless of whether you put a forward declaration inside a namespace, it could be that you're actually creating two versions of the symbol: one inside the namespace, and one in the global namespace. But you haven't provided enough code for me to tell. A forward declaration is used to avoid an
#include
for a symbol that is used in name only, which means that it is only used in a reference or pointer (name&
orname*
). In such a case, the compiler doesn't need to know the size of the class or any of its members; it only needs to know that the symbol exists.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.So if it is in global space , outside the namespace, would is work in both areas? The key would be "global". If it does not , then your explanation is either invalid or I need to implement the class (provide more code ) - then forward declaration is not required in the first place. I did say it is a TEST code , hence there is nothing to see since the cLass is not yet used / defined. My current "proof of concept " is therefore limited to "compiles and runs". I need to digest the second part of your post - since actual use - definition / declaration the class in question - does not compile ...
-
So if it is in global space , outside the namespace, would is work in both areas? The key would be "global". If it does not , then your explanation is either invalid or I need to implement the class (provide more code ) - then forward declaration is not required in the first place. I did say it is a TEST code , hence there is nothing to see since the cLass is not yet used / defined. My current "proof of concept " is therefore limited to "compiles and runs". I need to digest the second part of your post - since actual use - definition / declaration the class in question - does not compile ...
If
symbol
is declared in the global namespace, it's still visible from other namespaces. If it's also declared in a named namespace, that's the declaration that would be picked up within that namespace unless qualified as::symbol
. You're right that a forward declaration isn't required if you define the class--in the same file. There are times when an interface (.h) forward-declares a class that it uses in name only, and which is private (that is, implemented in its .cpp). This is done when using the PIMPL idiom[^].Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.