Convert to void *

how can i convert any object of my own class conversion to a pointer to void?

MyClass obj; (void*)obj; // Fail 
+4
source share
5 answers
 MyClass obj; void *p; p = (void*)&obj; // Explicit cast. // or: p = &obj; // Implicit cast, as every pointer is compatible with void * 

But be careful! obj is allocated on the stack so that as soon as you leave the function, the pointer becomes invalid.

Edit : Updated to show that explicit casting is not required in this case, since each pointer is compatible with the void pointer.

+10
source

If you use an address, you can convert it to a void pointer.

 MyClass obj; void *ptr = (void*)&obj; // Success! 
+5
source

You cannot convert a pointer to void* . You need to convert the pointer to your object in void*

 (void*)(&obj); //no need to cast explicitly. 

that the conversion is implicit

 void* p = &obj; //OK 
+5
source

To do something that has at least some chance of being meaningful, you must first take the address of the object by getting the value of the pointer; then draw a pointer.

 MyClass obj; MyClass * pObj = &obj; void * pVoidObj = (void*)pObj; 
+1
source

i beleive can you convert a pointer to an object only to a pointer to void ???

Perhaps: (void*)(&obj)

+1
source

All Articles