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. cast void* to class-pointer
Forum Updated to NodeBB v4.3 + New Features

cast void* to class-pointer

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.6k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    themts
    wrote on last edited by
    #1

    Hey guys,

    I have a question that is more c++ related then QT.

    I'm currently exporting a c++ class to a library.
    I have some functions where I want to return a pointer to an object that I can pass to another function as a handler. This was a common procedure I used in C.

    e.g.

    bool getDevice(NDevice *networkDevice);
    
    void startConnection(NDevice *networkDevice);
    

    As I don't want to export all the needed classes (in the example NDevice), I would change the arguments to void*
    In this case I would have to cast void* to my real class NDevice* back and forth.
    Is there a save way of casting which checks if void* is NDevice* ?
    dynamic_cast is not working on void* and static_casting is not safe as I could pass anything.

    CU

    JonBJ 1 Reply Last reply
    0
    • T themts

      ? Sorry, don't get it

      NDevice is a class.
      In this sample:
      bool getDevice(NDevice *networkDevice) would create an object of NDevice and return it via argument.

      But in this case I also need to supply the NDevice header.
      Or am I totally wrong here?

      I want to avoid exporting unnecessary header files as I would have to clean them up by using PIMPL first. I simply want to avoid this overhead-work as it is not necessary to know the Class-definition as long as I'm only using it as a handler.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #5

      @themts said in cast void* to class-pointer:

      bool getDevice(NDevice *networkDevice)

      Because you are only using a pointer to a class instance here, the compiler does not need the definition of NDevice. (You must simply precede this line with class NDevice; somewhere, to tell compiler "there is a class named NDevice".)

      That would not be the case If, say, the declaration were bool getDevice(NDevice networkDevice) or bool getDevice(NDevice &networkDevice). (Then it would need the full class definition for NDevice.)

      You can use this to allow .h files to use external classes without needing their definition. Selected .cpp files which need the internals can include the full definition where needed.

      1 Reply Last reply
      1
      • T themts

        Hey guys,

        I have a question that is more c++ related then QT.

        I'm currently exporting a c++ class to a library.
        I have some functions where I want to return a pointer to an object that I can pass to another function as a handler. This was a common procedure I used in C.

        e.g.

        bool getDevice(NDevice *networkDevice);
        
        void startConnection(NDevice *networkDevice);
        

        As I don't want to export all the needed classes (in the example NDevice), I would change the arguments to void*
        In this case I would have to cast void* to my real class NDevice* back and forth.
        Is there a save way of casting which checks if void* is NDevice* ?
        dynamic_cast is not working on void* and static_casting is not safe as I could pass anything.

        CU

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @themts said in cast void* to class-pointer:

        As I don't want to export all the needed classes (in the example NDevice), I would change the arguments to void*

        Are you aware that all you need to write is

        class NDevice;`
        

        and not go via void * here?

        1 Reply Last reply
        1
        • T Offline
          T Offline
          themts
          wrote on last edited by themts
          #3

          ? Sorry, don't get it

          NDevice is a class.
          In this sample:
          bool getDevice(NDevice *networkDevice) would create an object of NDevice and return it via argument.

          But in this case I also need to supply the NDevice header.
          Or am I totally wrong here?

          I want to avoid exporting unnecessary header files as I would have to clean them up by using PIMPL first. I simply want to avoid this overhead-work as it is not necessary to know the Class-definition as long as I'm only using it as a handler.

          JonBJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            What @JonB talks about is forward declaration.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • T themts

              ? Sorry, don't get it

              NDevice is a class.
              In this sample:
              bool getDevice(NDevice *networkDevice) would create an object of NDevice and return it via argument.

              But in this case I also need to supply the NDevice header.
              Or am I totally wrong here?

              I want to avoid exporting unnecessary header files as I would have to clean them up by using PIMPL first. I simply want to avoid this overhead-work as it is not necessary to know the Class-definition as long as I'm only using it as a handler.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @themts said in cast void* to class-pointer:

              bool getDevice(NDevice *networkDevice)

              Because you are only using a pointer to a class instance here, the compiler does not need the definition of NDevice. (You must simply precede this line with class NDevice; somewhere, to tell compiler "there is a class named NDevice".)

              That would not be the case If, say, the declaration were bool getDevice(NDevice networkDevice) or bool getDevice(NDevice &networkDevice). (Then it would need the full class definition for NDevice.)

              You can use this to allow .h files to use external classes without needing their definition. Selected .cpp files which need the internals can include the full definition where needed.

              1 Reply Last reply
              1
              • T Offline
                T Offline
                themts
                wrote on last edited by
                #6

                ah ok, wasn't aware of it.

                but when I make my .h file like this:

                class NDevice;
                bool getDevice(NDevice *networkDevice);
                

                and my .cpp file like this:

                #include "ndevice.h"
                
                Class::getDevice(NDevice *networkDevice) 
                {
                  networkDevice->doSomething();
                }
                

                I get the error:

                error: variable has incomplete type 'Class::NDevice'
                note: forward declaration of  'Class::NDevice'
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by SGaist
                  #7

                  If you have it in a namespace, you have to forward déclaré it within that namespace.

                  But in your case, the forward declaration must be done outside of the class definition.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    themts
                    wrote on last edited by
                    #8

                    forget what I just wrote (stupid mistake). The forward declaration works fine.

                    Thank you guys!

                    1 Reply Last reply
                    1

                    • Login

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