c++ standard practice: virtual interface classes vs. templates

前端 未结 6 1230
梦如初夏
梦如初夏 2020-12-04 08:06

I have to make a decision regarding generalization vs polymorphism.

Well the scenario is standard: I want to make my monolithic interdependent code to be more modula

6条回答
  •  情书的邮戳
    2020-12-04 08:45

    It's something of a false opposition. Yes, the major use of inheritance and virtual functions is in iostreams, which are very old, and are written in quite a different style to the rest of the std libraries.

    But many of the "coolest" modern C++ libraries such as boost do make use of runtime polymorphism, they just use templates to make it more convenient to use.

    boost::any and std::tr1::function (formerly also from boost) are fine examples.

    They are both single item containers to something whose concrete type is unknown at compile-time (this is especially obvious with any, because it has its own kind of dynamic cast operator to get the value out).

提交回复
热议问题