cast void* to class-pointer
-
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
-
@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 withclass NDevice;
somewhere, to tell compiler "there is a class namedNDevice
".)That would not be the case If, say, the declaration were
bool getDevice(NDevice networkDevice)
orbool getDevice(NDevice &networkDevice)
. (Then it would need the full class definition forNDevice
.)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. -
-
? 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.
-
Hi,
What @JonB talks about is forward declaration.
-
@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 withclass NDevice;
somewhere, to tell compiler "there is a class namedNDevice
".)That would not be the case If, say, the declaration were
bool getDevice(NDevice networkDevice)
orbool getDevice(NDevice &networkDevice)
. (Then it would need the full class definition forNDevice
.)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. -
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'
-
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.