Results 1 to 4 of 4

Thread: Why forward declaration ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Why forward declaration ?

    I am wondering:
    -------------------------------------------------------

    In a ".h" header file I can declare something like:

    class QTextBrowser;

    and in the respective ".cpp" class I will add this:

    #include <QTextBrowser>

    ----------------------------------------------------------

    I can also declare the "#include" in the header file (I know it).


    Question:

    Why is it better to "#include" a class in a .cpp instead of including it in the .h ?
    Why it is not better ?

  2. #2
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why forward declaration ?

    @Mods: maybe this should be moved to the general c++ forum?

    @OP:
    Rule of thumb: If you can use a forward declaration, use it!

    This is because of the way includes work. If you include a file in a header file, it's definitions are available in all other files which include said header file. So, if you have the following piece of code, how should the compiler decide which string class to use?

    Qt Code:
    1. =========
    2. header1.h
    3. =========
    4.  
    5. #include <string>
    6. using namespace std;
    7.  
    8. =========
    9. header2.h
    10. =========
    11.  
    12. #include <header1.h>
    13.  
    14. namespace myns
    15. {
    16. class string
    17. {
    18. //... implementation
    19. };
    20. }
    21.  
    22. using namespace std;
    23.  
    24. ==========
    25. source.cpp
    26. ==========
    27.  
    28. int main(int argc, char** argv)
    29. {
    30. // which string shall the compiler use? std::string or myns::string?
    31. string x = "Hello World!";
    32. cout << x:
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Using forward declaration also improves the time used to build your application as less object files are linked to each other (if I recall correctly right now, had a few beers tonight).

  3. #3
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why forward declaration ?

    By including header files that you don't need, you are giving unecessary work to the preprocessor and hence slowing down your compile time.

    Any data type that you declare either by reference or by pointer does not need to have its header included. You can forward declare it and should always forward declare it.

    @Methedrine Never use 'using namespace xxx' in a header file. It is very dangerous!
    Save yourself some pain. Learn C++ before learning Qt.

  4. #4
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why forward declaration ?

    Quote Originally Posted by Chicken Blood Machine View Post
    @Methedrine Never use 'using namespace xxx' in a header file. It is very dangerous!
    Indeed, and I thought my example code would outline that problem nicely, but yeah I also spotted the problem with my example: Instead of "using namespace myns" in header2.h I wrote using namespace std again ;-) that of course leads to std::string being used.

Similar Threads

  1. Compile Errors
    By luffy27 in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 05:26
  2. Unable to execute in Debug Mode
    By Kapil in forum Installation and Deployment
    Replies: 38
    Last Post: 5th April 2006, 07:27
  3. Q3PopupMenu
    By raphaelf in forum Qt Programming
    Replies: 15
    Last Post: 6th March 2006, 17:57
  4. Qt 4.1 and KDE 3.5.1 on OSX 10.2.8
    By Ptero-4 in forum Installation and Deployment
    Replies: 6
    Last Post: 6th February 2006, 02:44
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.