Results 1 to 4 of 4

Thread: Class vs include

  1. #1
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Class vs include

    i'm reading the book "C++ Gui programming with Qt4" and i notice inside the code of the book,
    that sometimes inside the header files we use preprocessor command include:

    #include <QDialogButtonBox>

    and sometimes we use the declaration of class:

    class QList;

    Is there any rule when to use class and when to use class??

    Sorry for the question, and the language, and thanks in advanced.

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Class vs include

    You need to include the header file if you want to use the class (constructing instances, accessing members, ...).
    If it is unnecessary to know the structure of the class (because you only need pointers to that class), use the forward declaration.

  3. The following user says thank you to Infinity for this useful post:

    otttx33 (11th February 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Class vs include

    What you are seeing is a forward declaration. Like the poster above me mentioned, since the header only references the class, you only need to include the "class QList". However, you will notice that when the class is actually used (in the .cpp file for example, the #include <QList> must be included. People do this because it reduces compile times since you no longer need to include the header of a forward declared class in that files, only the file where the class (QList) is actually used. While not a big deal on pet projects, if you ever develop for a commercial entity, you're talking about serious time saved.

  5. The following user says thank you to hammonjj for this useful post:

    otttx33 (11th February 2014)

  6. #4
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Class vs include

    thank youvery much

Similar Threads

  1. Replies: 5
    Last Post: 13th January 2014, 09:24
  2. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  3. Replies: 1
    Last Post: 20th January 2012, 04:39
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. #include class
    By mickey in forum General Programming
    Replies: 6
    Last Post: 11th June 2006, 12:36

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
  •  
Qt is a trademark of The Qt Company.