1

How to cast a Pointer in C++ from class name in String?

Psuedocode:

int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr); 
// The class/struct name is passed in as String

Thank you

Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73

2 Answers2

0

I'm not a c++ guru but I have two ideas that may help with brainstorming:

  • May the use of the registry pattern as described here would be of help: Instantiate class from name?
  • Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
-1

I am not sure but this must work Only Void pointer or boost can help, if it happens

thing * p = something; // pointer to object
void * pv = p;          // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object

Maybe same kind of situation is while returning values from Threads Overall reflection is not possible in c++. Its just brainstroming.

Charlie
  • 4,827
  • 2
  • 31
  • 55
  • That looks almost entirely unrelated to what the OP is asking. – us2012 Sep 22 '13 at 23:24
  • I see you what you did here, however I don't think you understood the question - partially due to my bad wording. But as an example: Something like this `thing * p2 = static_cast<"thing *">(pv);` Do you see what I did there? That is the effect I am trying to get. – Mathew Kurian Sep 22 '13 at 23:41