Why is the virtual keyword needed?

前端 未结 4 1670
旧巷少年郎
旧巷少年郎 2021-01-13 07:17

In other words, why doesn\'t the compiler just \"know\" that if the definition of a function is changed in a derived class, and a pointer to dynamically allocated memory of

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 07:38

    One of the main designing principles of C++ is that C++ does not incur overhead for features that are not used (the "zero-overhead principle"). This is because of a focus on high performance

    This is why you need to opt in to features like virtual functions while in languages like Java, functions are virtual by default.

提交回复
热议问题