Original Post
Hello, I've been looking around here for solutions to my problem but I can't find any. So I'll make a new thread: I need a pointer, but it's not sure where it's going to point to. It may be going to point to an object of class SomeClass1 or to an object of class SomeClass2, etc. Here is some code of what i'm trying to do: The last line: (*FlexiblePointer).a = 15; gives me a compiler error: E2109 Not an allowed type I remember from delphi that you can cast an object (basically a pointer) to anything you'd like and if the conversion doesn't go properly you'll be accessing junk. Apparently it doesn't go this way in C++, could someone help me with this? Any help is greatly appreciated.
class SomeClass
{
public:
int a;
}
void main()
{
void * FlexiblePointer; // not pointing to an object of a specific class yet.
FlexiblePointer = new SomeClass;
// Now this FlexiblePointer is pointing to a SomeClass object.
// Unfortunetely the type of the FlexiblePointer is still (void *)
// So I need to cast it (SomeClass *), so I can access its data.
FlexiblePointer = (SomeClass *)(FlexiblePointer);
// Now that it's cast to (SomeClass *), I should be able to access its data:
(*FlexiblePointer).a = 15;
}