Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Forward declaration placement ?

Forward declaration placement ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 2 Posters 33 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    ....

    Greg UtasG 1 Reply Last reply
    0
    • L Lost User

      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

      ....

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      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& or name*). 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.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      L 1 Reply Last reply
      0
      • Greg UtasG Greg Utas

        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& or name*). 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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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 ...

        Greg UtasG 1 Reply Last reply
        0
        • L Lost User

          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 ...

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          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.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups