Home  >  Article  >  Backend Development  >  Similarities and differences between C++ inline functions and virtual functions

Similarities and differences between C++ inline functions and virtual functions

王林
王林Original
2024-04-17 09:39:01872browse

Inline functions embed the function body into the call point, which improves performance and code volume, but has lower readability; virtual functions call functions overridden by subclasses through polymorphism, improving flexibility, but have higher runtime overhead. .

C++ 内联函数与虚拟函数的异同

The similarities and differences between C inline functions and virtual functions

Inline functions

Inline functions are compiled The compiler embeds the function body into the call site when it is called, rather than the function that performs the function call process.

Advantages:

  • Improve performance: avoid calling overhead.
  • Reduce code size: The function body will not appear repeatedly in multiple call points.

Disadvantages:

  • Increased compilation time: the function body needs to be compiled for each call point.
  • Reduced code readability: function bodies are scattered throughout the code base.

Syntax:

inline int sum(int a, int b) {
  return a + b;
}

Virtual function

Virtual function is a function that achieves polymorphism through the inheritance mechanism . When a virtual function on a parent class object is called, the actual function called is determined by the object's dynamic type.

Advantages:

  • Realize polymorphism: subclasses can override the virtual functions of the parent class.
  • Improve the scalability and flexibility of the code.

Disadvantages:

  • Runtime overhead: Virtual function table maintenance and indirect calls need to be maintained.
  • Increased code complexity: virtual and override keywords are required.

Grammar:

class Base {
 public:
  virtual void print() {
    std::cout << "Base" << std::endl;
  }
};

class Derived : public Base {
 public:
  virtual void print() override {
    std::cout << "Derived" << std::endl;
  }
};

Comparison of similarities and differences:

Features Inline function Virtual function
Calling mechanism Function body embedding Indirect call
Performance Higher Lower
Code size Smaller Bigger
Readability Lower Higher
Polymorphism Not supported Supported

##Actual case:

You can use inline functions to implement simple mathematical operations, such as summation:

inline int sum(int a, int b) {
  return a + b;
}

int main() {
  std::cout << sum(1, 2) << std::endl;  // 输出:3
}

You can use virtual functions to implement the graphics drawing interface:

class Shape {
 public:
  virtual void draw() = 0;
};

class Circle : public Shape {
 public:
  virtual void draw() override {
    std::cout << "Drawing a circle" << std::endl;
  }
};

int main() {
  Shape* shape = new Circle();
  shape->draw(); // 输出:Drawing a circle
}

The above is the detailed content of Similarities and differences between C++ inline functions and virtual functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn