Create a derived class from a base class

I want to make a new object where the constructor of a derived class will accept the base class as its parameter and use it as a template assigning all the variables of the base class to the newly constructed derived class. I need an actual object as down-casting will only provide undefined behavior. I hope there is a simple way to do this.

One problem I ran into was that things like the copy constructor are all private and in the generated body. I wanted to use the template variable in the New Object function however it only works if it is the same class type.

To add context to what I’m doing, I’m trying to deserialize a large hierarchy from json. Due to it’s size, the derived classes shouldn’t have to re-implement the deserialization code but rather have the base class do it and pass itself down as the base object then the derived will use its variables to create itself and so on. The problem just lies in using the base class without casting.

Hi dgiangrave,

Is there a reason why your base class would need a reference to itself as the child class? Can you not perform the common deserialization in the base class and then the child-specific deserialization in the child classes?

Hi dgiangrave,

We haven’t heard from you in a while, so I’m going to mark this issue as resolved for tracking purposes. If you are still encountering your issue, feel free to open the question back up.