Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it possible to type cast a base class to a child class using a string?

Something like this maybe? Please
std::map<cstring,baseclass> types;
types["childClass1"] = childClass1;
.
.
.
CString nameOfClass = getStringFromCLI();

types[nameOfClass] * aChildClass = (types[nameOfClass] *) new baseClass();
//Now aChildClass is of type childClass1 (NOT baseClass)


Right now I have hundreds of thousands of child classes, and needless to say, I use a script to generate if statements and type cast the baseClass accordingly.

All ideas are welcome! but you don't have to mention that my code is worthless cause I already know that.

Here is how you can do this in Java (from http://www.knowledgesutra.com/forums/topic/32520-can-i-cast-a-string-as-a-class-object/[^]
import java.lang.reflect.*;
import java.awt.*;
......
//Your class in string
 String s="<your class>";
//Get class definition
 Class cls = Class.forName(s);
//Instantiate
 Object obj=cls.newInstance();
Posted
Updated 12-Jan-11 10:56am
v2
Comments
dasblinkenlight 12-Jan-11 16:47pm    
Suppose you manage to upcast the pointer to the child class. I still do not see how is it useful, since the static type of *aChildClass remains baseClass. Your script-generated code can make good use of the new type because the pointer inside the 'if' block is statically typed to the child type, but your pseudocode wouldn't be able to do the same, because it does not have thousands of 'if' blocks. A good way to see what I mean would be to try providing an example of what happens after the last line of your pseudocode: you wouldn't be able to call aChildClass->aChildMethod(), unless aChildMethod is in the base class.

Once upon a time there was something called taligent, some code ended up in the java world, some ended up as part of IBM's C++ class library. I havent looked at that library for many years, but if I remember correcty there was something there that might have done something akin to what I think you want. This was in the days before static_, dynamic_, cast became a part of the c++ language, but the functionality of dynamic_cast was needed.

If I remember correctly each class definition contained a macro that added an entry to a global map for each class and it's parent class. This defined the inheritance tree for the entire library in an extensible manner.

As I said - if I remember correctly ...

The functionality was used to simulute dynamic_cast, as in validate that it was "right" to cast from one type to another, and to retrieve the name of the type for a given object.

This method only supported single inheritance, and a cast was considered valid if it was from a base class to a descandant class. The object had to be of, or descend from, the target type of the cast.

Not that I would advice you to do anything like that today.
C++ Primer[^], by Stanley B. Lippman makes for an excellent reading.

Update:
Java supports reflection

Take look at this article:
Metaclasses and Reflection in C++[^]

The map mentioned earlier was a very simplistic metadata container ...

Regards
Espen Harlinn
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 12-Jan-11 18:49pm    
Espen, in few words -- most interesting and elaborate answer (my 5, could be many 5s).
Thank a lot for the interesting reference.
Unfortunately, in my opinion this is a big overkill, considering the level of ignorance around this particular question (just look around); however, I perfectly understand you -- I do overkill myself all the time.

You interest and knowledge in a great thing -- it could be a topic of a big article. I started using meta-class techniques from Delphi, where it was nicely embedded. (Actually, later on I created whole tehnology and few variations of meta-data driven development cycle, but this is a topic somewhat different from meta-classes.) One of two biggest problems of .NET/C#, etc. is the lack of true meta-classes (The type Type is only a rudimentary, "untyped" meta-class). Most key .NET ideas were brought from Delphi to .NET by their architect (of the both), but the meta-class remains at the "almost there" status. In CodeProject, I barely touched this problem in one of my articles of my 3-part series on enumeration (see http://www.codeproject.com/KB/dotnet/EnumerationCommandLine.aspx). If you want to discuss related problem, you're welcome to contact me directly via may Web site (there is nothing interesting on the topic there, only a contact page), you can find by my CodeProject profile.
Best, SA
Nuri Ismail 12-Jan-11 18:49pm    
Excellent answer! Have a 5! :)
The short answer is NO! There is no support for reflections in C++, so you cannot achieve it in the Java-way (or any other language-way that supports reflections. Class, Method and other classes are a part of Java reflections).
Consider redesigning your architecture; you may want to consider C++ meta-programming, or a more robust object-oriented design.
Personally, I think one should never need to use any kind of reflection in C++ unless there are serious flaws in his/her design.

Ask more, if you need a further help.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jan-11 18:19pm    
Arman, you have more patience. My 5.
Nuri Ismail 12-Jan-11 18:51pm    
Good call!
No, not even close.

You need to know better what classes are, and only then what the cast is supposed to do. Hm... I don't even know where to start. Perhaps read and try to code something very basic first.
 
Share this answer
 
Comments
Ribhi Kamal 12-Jan-11 16:44pm    
I know that is not even close...lol I just used the code above as a way to make the question clear.
Sergey Alexandrovich Kryukov 12-Jan-11 17:23pm    
Hey, Shakazzolo, you did not get it... it was not about your pseudo-code.
You have a full right to use pseudo-code.
That was about your idea -- it is based on deep misunderstanding what a type and class are; you also probably don't really know difference between type cast and conversion.

I assume that was your vote, huh?
Sergey Alexandrovich Kryukov 12-Jan-11 18:18pm    
Oh, "3" changed to "1"? Naturally. Ignorant, coward anonymous should be also aggressive -- those traits always come together. I am not afraid to hurt someone's feeling -- why? -- if I don't know who am I talking to...
Nuri Ismail 12-Jan-11 18:49pm    
I'll try to compensate this ignorant vote with my 5! :)
Sergey Alexandrovich Kryukov 12-Jan-11 18:51pm    
Thank you, Nuri. I knew you would understand things properly, as I saw your own posts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900