Results 1 to 4 of 4

Thread: call a class

  1. #1
    Join Date
    Jun 2008
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default call a class

    what is the difference between declaring a class

    this way:
    Qt Code:
    1. #include <QDialog>
    2. #include <QHash>
    To copy to clipboard, switch view to plain text mode 

    or, this way:
    Qt Code:
    1. class QFile;
    2. class QFtp;
    3. class QLabel;
    4. class QLineEdit;
    5. class QTreeWidget;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2010
    Posts
    99
    Thanks
    31
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: call a class

    In first code snippet you are including the class by #include directive.

    In second code snippet its forward declaration. You are just letting the compiler (or preprocessor am not sure which one) know that QFile is a class but I don't want to define it here or include it here. Then in you .cpp file you do and #include<QFile> which points to actual QFile class.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: call a class

    As a practical matter, the first way includes the contents of the file it references, and the compiler has to grind through the contents of that file - which may be a significant amount of code. In the second case, the compiler only handles the single line of code. The end result is decreased compilation time if you use the second method.

    Whether this is worthwhile is questionable. I typically include <Qt/GUI> in my headers, and despite the enormous amount of code this hauls in, my compilation times are still very quick, even on large projects. The total gain by using the second method would only be a matter of a few seconds, at best.

    Also, note that in the second case you can only declare variables of the type specified; you have no access to their internal structure, because the compiler knows nothing about it. So it isn't as useful when you need to reference some internal class member.

    Try it both ways. If you see a significant improvement in compilation speed with the second, and don't run up against it's limitations, go ahead and use it.

  4. #4
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: call a class

    When you include the relative file, you are just "copying" the class definition, so the compiler knows everything about it. When you use the forward declaration, the compiler knows only that there is a class with that name. You can not declare a variable of that type, only a pointer or a reference (since the compiler does not know even how big is the class). Of course when you actually use that class (in the .cpp file) the compiler must know it, so you have to include the file there.

    Forward declarations can be used for reduce compilation time, but also to reduce compiled files: if you have a header A.h which is included in B.h which is included in C.cpp, when you modify A.h you need to recompile C.cpp. If you used a forward declaration, no.
    I have no idea if there are issues using forward declarations with Qt.

    If you are worried about compiling speed, try precompiled headers.

Similar Threads

  1. Replies: 6
    Last Post: 15th April 2010, 20:02
  2. How to call super class method from sub class
    By mkkguru in forum Qt Programming
    Replies: 9
    Last Post: 4th February 2010, 05:29
  3. How to call the dll's C++ class in Qt program?
    By tszzp in forum Qt Programming
    Replies: 1
    Last Post: 2nd February 2010, 09:38
  4. Replies: 3
    Last Post: 16th May 2007, 11:07
  5. How to call my QPushButton from another class ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2006, 12:41

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.