Forward declarations and header inclduding for some objects like QString



  • Hello,

    I looked at this question

    https://forum.qt.io/topic/37584/forward-declarations-for-objects-that-are-not-widgets

    and noticed latest reply was

    @cincirin said in Forward declarations for objects that are not widgets:

    You can use forward declaration for variables only for pointers and references. You must include the header if variable is not declared as pointer or reference. Besides compilation speed forward declaration is used for mutual recursion.

    which I read many articles online. However, practicing with qtcreator and qt widgets applications I came by QString class and was surprised that I can ONLY forward declare QString class in .h file of class that I'm creating. Of course I have to #include <QString> in the .cpp file of my class but how that is possible while something like this is NOT

    ////////////////////////////////////////////////A.h
    
    #ifndef AA
    #define AA
    class B;
    class A
    {
    public:
    
    	B bobj;                      //this will not work unless it's pointer or reference object
    	A(B);
    };
    #endif
    
    ////////////////////////////////////////////////B.h
    
    #ifndef BB
    #define BB
    class B
    {
    public:
    	int xb = 15;
    	B();
    };
    #endif
    
    ////////////////////////////////////////////////A.cpp
    
    #include "A.h"
    #include <iostream>
    #include "B.h"
    A::A(B f): bobj(f)
    {
    	std::cout << bobj.xb << " from A" << std::endl;
    }
    
    ////////////////////////////////////////////////B.cpp
    
    #include "B.h"
    #include <iostream>
    B::B()
    {
    	std::cout << xb << std::endl;
    }
    
    ////////////////////////////////////////////////Main.cpp
    
    #include <iostream> 
    #include "A.h"
    #include "B.h"
    
    int main()
    {
    	B m;
    	A ff(m);
    	return 0;
    }
    

    I know it's probably c++ understanding issue but I hope someone would kindly clear this for me please. Thank you :)

    Edit: Apology for the duplication, I got the warning that the other question is old and I assumed it's better option to create new one.


  • Moderators

    However, practicing with qtcreator and qt widgets applications I came by QString class and was surprised that I can ONLY forward declare QString class in .h file of class that I'm creating

    The key here is the "widgets applications" part. If you substitute e.g. class B in your example with QString it will still not compile. That's how it works and there's no magic here.
    The thing is that when you create an actual app your main file doesn't look like in your example. It looks more like this:

    #include <QApplication> //or QWidget, QDialog, QObject or whatever
    #include "your_class_with_fw_declared_qstring.h"
    
    int main(int argc, char *argv[]) {
    

    Remember that compilers don't usually compile .h files. Headers are sorta "copy/pasted" into the spot they are included, so what you really get is:

    //All the stuff from Qt headers, which is also everything from QObject, which includes QString among other stuff
    
    class QString; //this is your forward declaration, but QString is already declared fully by the above so this is just ignored
    class YourClass() {
    ...
    
    int main(int argc, char *argv[]) {
    ...
    

    So to the compiler there's no problem. QString is declared fully at the place you declare your class.



  • @Chris-Kawa I'm totally thankful for your clear explanation. My regards :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.