I am trying to use the memory address of an object to create a unique ID for it. I want to be able to use that unique ID(which is the memory address, I think) to reference the object and use it.
How can I cast it back to a pointer, I've tried but I don't get the right value.
Also I'm sure these is a more efficient way of creating an ID of all my objects that this?(Using their memory addresses so I can access them easily still)?
One problem I see is that at line 21 you're passing unknown by value, which means you have a temporary copy of the UnknownObject instance on the stack. At line 23, you take the address of this temporary object. id now contains the address of the temporary object. Line 27, this temporary object goes out of scope, invalidating id.
As pointed out by coder777, your solution is not portable. You will have problems if you switch to a 64 bit compiler.
I really question why you're trying to do this. Do you understand polymorphism? If you really want a unique identifier for each object, I would suggest making a base class that all your other classes derive from. Then a pointer to your base class will uniquely identify anything derived from your base class (and is a portable solution).