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. Does a QSharedPointer have to include headers rather than a forward declaration ?
Forum Update on Monday, May 27th 2025

Does a QSharedPointer have to include headers rather than a forward declaration ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 943 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.
  • MartinChan3M Offline
    MartinChan3M Offline
    MartinChan3
    wrote on last edited by
    #1

    Before I use QSharedPointer, I am used to use forward declaration to declare my class instead of include its .h in my header file code like :

    class MyClass;
    
    class Controller {
    private:
          MyClass* myClassp; 
    };
    

    But when I start using QSharedPointer, and I have to use the traditional way, which means u have to include its .h file like:

    #include "myclass.h"
    
    class Controller {
    private:
          QSharedPointer<MyClass> myClass;
    };
    

    Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.

    jsulmJ kshegunovK 2 Replies Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      unless the meta system does something weird with the Qt implementation of shared pointers, it should work. Forward declarations are an integral part of C++.

      Shared pointers are usually declared in a function scope so that they automatically delete the object. Your use of a shared pointer as a class variable is, IMHO, not the right way. If you are declaring it as a class variable then just created the object in the contstructor and delete it in the destructor. No need for a private shared pointer as a class variable.

      MartinChan3M 1 Reply Last reply
      1
      • MartinChan3M MartinChan3

        Before I use QSharedPointer, I am used to use forward declaration to declare my class instead of include its .h in my header file code like :

        class MyClass;
        
        class Controller {
        private:
              MyClass* myClassp; 
        };
        

        But when I start using QSharedPointer, and I have to use the traditional way, which means u have to include its .h file like:

        #include "myclass.h"
        
        class Controller {
        private:
              QSharedPointer<MyClass> myClass;
        };
        

        Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @MartinChan3 Forward declarations only work as long as you only use your class to declare a pointer. To declare a pointer compiler does not have to know anything about the class as a pointer is always the same thing. But if you use the class itself the compiler needs to know what this class is, so you need to include header file.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MartinChan3 said in Does a QSharedPointer have to include headers rather than a forward declaration ?:

          But when I start using QSharedPointer, and I have to use the traditional way

          What error message do you get? Since you don't have a ctor (in your example) of the class, the compiler generates one in the header. And since there the dtor of myClass must be called, myClass must be visible.

          In the first example without the shared pointer you most likely leak myClasss.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • Kent-DorfmanK Kent-Dorfman

            unless the meta system does something weird with the Qt implementation of shared pointers, it should work. Forward declarations are an integral part of C++.

            Shared pointers are usually declared in a function scope so that they automatically delete the object. Your use of a shared pointer as a class variable is, IMHO, not the right way. If you are declaring it as a class variable then just created the object in the contstructor and delete it in the destructor. No need for a private shared pointer as a class variable.

            MartinChan3M Offline
            MartinChan3M Offline
            MartinChan3
            wrote on last edited by
            #5

            @Kent-Dorfman Thx,I will avoid using QSharedPointer in member variables of my class. And thanks all above.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @MartinChan3 said in Does a QSharedPointer have to include headers rather than a forward declaration ?:

              I will avoid using QSharedPointer in member variables of my class.

              Why?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • MartinChan3M MartinChan3

                Before I use QSharedPointer, I am used to use forward declaration to declare my class instead of include its .h in my header file code like :

                class MyClass;
                
                class Controller {
                private:
                      MyClass* myClassp; 
                };
                

                But when I start using QSharedPointer, and I have to use the traditional way, which means u have to include its .h file like:

                #include "myclass.h"
                
                class Controller {
                private:
                      QSharedPointer<MyClass> myClass;
                };
                

                Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                @MartinChan3 said in Does a QSharedPointer have to include headers rather than a forward declaration ?:

                Can I use the forward declaration and QSharedPointer at a same time ? Due to I want the convience of both way.

                Define the big four for your class (the one with the QSharedPointer member) and it's going to be fine.

                Read and abide by the Qt Code of Conduct

                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