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 ?

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 941 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.
  • M Offline
    M Offline
    MartinChan3
    wrote on 1 Jul 2020, 01:30 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.

    J K 2 Replies Last reply 1 Jul 2020, 04:20
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 1 Jul 2020, 03:13 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.

      M 1 Reply Last reply 1 Jul 2020, 05:23
      1
      • M MartinChan3
        1 Jul 2020, 01:30

        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.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Jul 2020, 04:20 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
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Jul 2020, 04:25 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
          • K Kent-Dorfman
            1 Jul 2020, 03:13

            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.

            M Offline
            M Offline
            MartinChan3
            wrote on 1 Jul 2020, 05:23 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
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 1 Jul 2020, 07:26 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
              • M MartinChan3
                1 Jul 2020, 01:30

                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.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 1 Jul 2020, 20:53 last edited by kshegunov 7 Jan 2020, 20:53
                #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

                6/7

                1 Jul 2020, 07:26

                • Login

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