Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Need for Forward declaration
Forum Updated to NodeBB v4.3 + New Features

Need for Forward declaration

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 794 Views
  • 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    I am following the book , C++ GUI Programming with Qt4. In chapter 2 , there's a mention of forward declaration. For example

    #ifndef....
    #define ....
    
    #include <QDialog.h>
    
    class QCheckbox;
    class QPushButton;
    class QLineEdit;
    .......
    

    Can't these be used as follows;

    #include <QDialog.h>
    #include<QCheckbox>
    #include<QPushButton>
    #include<QLineEdit>
    
    

    I've always tried with header files and some of my programs have run successfully. So, can somebody differentiate between these two methods?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    Swati777999S jsulmJ 2 Replies Last reply
    0
    • Swati777999S Swati777999

      I am following the book , C++ GUI Programming with Qt4. In chapter 2 , there's a mention of forward declaration. For example

      #ifndef....
      #define ....
      
      #include <QDialog.h>
      
      class QCheckbox;
      class QPushButton;
      class QLineEdit;
      .......
      

      Can't these be used as follows;

      #include <QDialog.h>
      #include<QCheckbox>
      #include<QPushButton>
      #include<QLineEdit>
      
      

      I've always tried with header files and some of my programs have run successfully. So, can somebody differentiate between these two methods?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @Swati777999 Forward declarations can help to break circular dependency: in header a you include header b and in header b you include header a -> circular dependency. You solve this using forward declarations, then you do not have to include the header file (you, of course, need to include it in cpp file, but this does not lead to circular dependency).

      Forward declarations also help to speed up compalation time, because you reduce the amount of includes in your header files. Each time you include a header file the preprocessor replaces the include statement with the content of the header file, but if the header file includes other header files then the amount of included text increases, which makes build times longer.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      6
      • Swati777999S Swati777999

        I am following the book , C++ GUI Programming with Qt4. In chapter 2 , there's a mention of forward declaration. For example

        #ifndef....
        #define ....
        
        #include <QDialog.h>
        
        class QCheckbox;
        class QPushButton;
        class QLineEdit;
        .......
        

        Can't these be used as follows;

        #include <QDialog.h>
        #include<QCheckbox>
        #include<QPushButton>
        #include<QLineEdit>
        
        

        I've always tried with header files and some of my programs have run successfully. So, can somebody differentiate between these two methods?

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by Swati777999
        #2

        @Swati777999

        So the text says, for private variables, forward declaration of their classes is done as they are pointers and need to be accessed in the header files. Writing in terms of forward declaration speeds up the process of execution by the compiler.

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        1 Reply Last reply
        0
        • Swati777999S Swati777999

          I am following the book , C++ GUI Programming with Qt4. In chapter 2 , there's a mention of forward declaration. For example

          #ifndef....
          #define ....
          
          #include <QDialog.h>
          
          class QCheckbox;
          class QPushButton;
          class QLineEdit;
          .......
          

          Can't these be used as follows;

          #include <QDialog.h>
          #include<QCheckbox>
          #include<QPushButton>
          #include<QLineEdit>
          
          

          I've always tried with header files and some of my programs have run successfully. So, can somebody differentiate between these two methods?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @Swati777999 Forward declarations can help to break circular dependency: in header a you include header b and in header b you include header a -> circular dependency. You solve this using forward declarations, then you do not have to include the header file (you, of course, need to include it in cpp file, but this does not lead to circular dependency).

          Forward declarations also help to speed up compalation time, because you reduce the amount of includes in your header files. Each time you include a header file the preprocessor replaces the include statement with the content of the header file, but if the header file includes other header files then the amount of included text increases, which makes build times longer.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          6

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved