Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Forward declarations and header inclduding for some objects like QString

Forward declarations and header inclduding for some objects like QString

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.8k 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.
  • A Offline
    A Offline
    Anddo
    wrote on last edited by Anddo
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      3
      • Chris KawaC Chris Kawa

        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.

        A Offline
        A Offline
        Anddo
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

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