'c++ base class object to call unknown methods in subclasses

I am facing this problem:

An upstream application defines a class (e.g. box), and a member (say property) with a base class type. I would make a derived class for that member, add new members and methods without updating their application.

Essentially I do box->property = make_shared<myProperty>(). Is there a way to keep the interface of calling the members and methods the same? That is, to access a property using box->property->length or box->property->GetWeight(), rather than dynamic_pointer_cast<myProperty>(box->property)->GetWeight(). The challenge here is they won't update the base property class, and I am not supposed to change box. But we wish to keep the interface the same so our customers won't complain.

Is it possible? If not, how could we do to best keep the main app and my plugin relatively independent while minimize the changes on the customer side? Any suggestions are welcome.



Solution 1:[1]

Looks to me like the derived class for that member property violates Liskov's substitution principle.

You mentioned not being able to modify the Box class. But are you allowed to modify the property base class? I suggest you add your "additional" methods of your derived class to the property base class.

The intent here being that the interface between the base and derived class should be one and the same. So do this only if it makes sense design wise.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wyy