Imagine that you have a derived class where the base class is something that you cannot change. The base class has many states (many non-constant private members) and many constructors with different number of arguments to initialize a certain subset of the state (the size of the subset depends on the constructor, of course).
Now my derived class is a very light wrapper over the base class. Suppose that it does not add any state of its own and only slightly changes the behavior of a pair of methods (it may make several additional protocols around the call to super.originalMethod() ).
The problem is that I want to take an object of the base class and create a “copy” of it with the same state, but as an instance of my derived class.
It's complicated. I cannot name the "most complete" constructor of the base class, passing all the state from the source, calling getters, because depending on how the base class was created, some of the state values may be rejected by this constructor. For example, you can create a default object with 0-arg ctor, any number of values will be zero. However, it is not legal to pass null values to ctor, which allows you to specify these values.
In addition, the method described above is fragile because if a modification of the base class occurs that adds more state and an “even more complete” constructor (or a state that cannot be set in the constructor, but only through access methods), the copy will no longer be filled .
What I want is similar to `clone (), but rather, it initializes a new object of the same type, initializes the members of the base class of the derived class. I think this does not exist. Any template suggestions that can offer something equivalent?
Keep in mind that I cannot change the base class. If I could, it would be a lot easier.