Is there any reason not to make a member function virtual?

后端 未结 7 1250
既然无缘
既然无缘 2020-12-14 07:55

Is there any real reason not to make a member function virtual in C++? Of course, there\'s always the performance argument, but that doesn\'t seem to stick

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 08:41

    Stroustrup, the designer of the language, says:

    Because many classes are not designed to be used as base classes. For example, see class complex.

    Also, objects of a class with a virtual function require space needed by the virtual function call mechanism - typically one word per object. This overhead can be significant, and can get in the way of layout compatibility with data from other languages (e.g. C and Fortran).

    See The Design and Evolution of C++ for more design rationale.

提交回复
热议问题