I'm trying to do something like a runtime-defined function return type in C++.
I have four different classes (actually, a lot more, but let's make this problem simple), the class Factory, the class Parent and two child classes FirstChild and SecondChild. Both FirstChild and SecondChild extend Parent. In the actual code there are many more child classes, but they work the same.
In the Factory class, I have the read method, that needs an int ID input and returns one of the child classes.
Normally what could be done, and that's what's rederred here, is that I could return the Parent method with a pointer and use that, but the fact is that the child classes have some parameters that are not in common with the Parent class.
Also, when I call the method Factory::read with the ID, I do not know prior what will be the child result class that I'll have, but I have a list of possibilities of what it can be (remember that in the actual code there are way more than only two child classes), so I can't do a dynamic_cast.
For those who want to know, I'm trying to make a MTProto (the protocol of Telegram) client in C++, and I'm stuck with the parsing of the TLObjects.
If anyone has a solution or an alternative path to do what I'm trying to do, any help will be gladly appreciated!