Why do we need virtual functions in C++?

前端 未结 26 3668
北恋
北恋 2020-11-21 05:50

I\'m learning C++ and I\'m just getting into virtual functions.

From what I\'ve read (in the book and online), virtual functions are functions in the base class that

26条回答
  •  萌比男神i
    2020-11-21 06:12

    About efficiency, the virtual functions are slightly less efficient as the early-binding functions.

    "This virtual call mechanism can be made almost as efficient as the "normal function call" mechanism (within 25%). Its space overhead is one pointer in each object of a class with virtual functions plus one vtbl for each such class" [A tour of C++ by Bjarne Stroustrup]

提交回复
热议问题