📜  C++中的虚函数和内联函数之间的区别

📅  最后修改于: 2021-05-30 13:27:59             🧑  作者: Mango

虚函数: 虚函数是在基类中声明并由派生类重新定义的成员函数。

内联函数: 内联函数是由关键字inline定义的常规函数,它是由编译器扩展的短函数,其参数仅计算一次。

在C++中定义函数内联的语法为:

inline return-type function-name(parameters)
{
    // function code
} 

虚函数与内联函数的区别如下:

Virtual function

Inline function

1. Virtual function must be declared in public section of class. 1. Inline function is a normal function which is defined by the keyword inline.
2. Virtual function cannot be static. 2. Inline function can also be non-static.
3. Virtual function is defined in base class. 3. Inline function are the short length functions that are automatically made the inline functions without using the inline keyword inside the class.
4. Virtual function are decrease the efficiency of code. 4. Inline function are used to increase the efficiency of code.
5. Virtual function is to run time polymorphism. 5. Inline function is to compile time polymorphism.
6. Virtual function may consists of virtual destructor but it cannot have a virtual constructor.  6. Inline function can also consist of inline constructor.
7. Virtual may use for dynamic linkage. 7. Inline function is used to reduce the function call overhead.
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”