I get several objects of type Foo from an external API call. Locally, I want to process these objects with a little added information, so I have a subclass of FooSon that adds these extra fields. How can I convert all of the objects that I get into my new inherited type? Downcasting does not seem to be an option because these objects are not really FooSon .
The only solution I developed is to create a conversion function that takes a Foo object as an argument and then copies all public / protected values to a new FooSon object, which is then returned.
Disadvantages:
- Loss of information (private values)
- You need to adapt the conversion function if Foo is ever changed.
The Foo class does not implement the copy constructor or clone operator. I have the source code for Foo, but I would like to avoid changing it in order to maintain compatibility with future versions. However, if this is the only viable alternative, I would modify the Foo implementation to get what I need.
source share