且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设计模式,将数据添加到类(第三方)而不修改

更新时间:2023-09-19 16:38:22

为什么不要继承是扩展类的一种方式,无论是行为还是状态。除非您只想将类的实例与其他数据相关联(在这种情况下,它根本不会扩展)和std ::地图是正确的答案。

Why don't you just subclass the class? Inheritance is the way to extend classes, whether with behavior or state. Unless you just want to associate instances of the class with other data, in which case it's not extending at all, and a std::map is the right answer.