C++ Class Extension

前端 未结 9 1203
既然无缘
既然无缘 2020-12-11 03:22

Is there a way to add new methods to a class, without modifying original class definition (i.e. compiled .lib containing class and corresponding .h file) like C#\'s class ex

9条回答
  •  独厮守ぢ
    2020-12-11 03:53

    There is one way in which it can be done. And that's by relaxing your requirements a bit. In C++, people often say that the interface of a class consists not just of its member functions, but of all functions that work on the class.

    That is, non-member functions which can be given the class as a parameter should be considered part of its interface.

    For example, std::find() or std::sort() are part of the interface of std::vector, even though they aren't members of the class.

    And if you accept this definition, then you can always extend a class simply by adding nonmember functions.

提交回复
热议问题