Home > Backend Development > C++ > body text

The application of C++ virtual functions in STL: revealing the polymorphic nature of containers

WBOY
Release: 2024-04-28 15:09:01
Original
1097 people have browsed it

STL containers use virtual functions to achieve polymorphism, allowing base class pointers to call derived class methods. Specific applications include destructors and operating member functions, such as operator[], push_back(), and erase(), to achieve dynamic binding and storage of different types of objects.

C++ 虚拟函数在 STL 中的应用:揭秘容器的多态本质

The application of C virtual functions in STL: revealing the polymorphic nature of containers

Preface

STL (Standard Template Library) is a powerful container library in C, which provides us with various data structures and algorithms. In STL, virtual functions are widely used to achieve dynamic polymorphism of containers. This article will delve into the application of virtual functions in STL and demonstrate its role through practical cases.

Introduction to virtual functions

Virtual functions are a polymorphic feature in C that allow objects of derived classes to call their own through pointers or references of base classes Method of implementation. This means that a derived class object can behave like its base class object and be treated as a base class object.

Application of virtual functions in STL

Containers in STL, such as vector, list and map, all use virtual functions to achieve their polymorphism. Specifically, they declare virtual functions in the destructor and some operating member functions, such as operator[], push_back(), erase(), etc.

Practical case: dynamic binding and derived class container

In order to demonstrate the role of virtual functions in STL, we create a base class named Base, and two Derived1 and Derived2 are derived classes.

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

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

class Derived2 : public Base {
public:
  virtual void print() const override { cout << "Derived2" << endl; };
};
Copy after login

Next, we create a vector and add Base, Derived1 and Derived2 objects to it.

vector<Base*> vec;
vec.push_back(new Base());
vec.push_back(new Derived1());
vec.push_back(new Derived2());
Copy after login

Now, let’s iterate through the vector and call the print() method of each object. Due to the use of dynamic binding, the base class pointer can automatically call the method of the derived class object to print the object's type in a polymorphic manner.

for (Base* obj : vec) {
  obj->print();
}
Copy after login

Output:

Base
Derived1
Derived2
Copy after login

As you can see from the output, the print() method is called correctly and the type of the derived class is printed. This demonstrates how virtual functions implement container polymorphism in STL.

Conclusion

Virtual functions are the key mechanism to achieve container polymorphism in STL. STL containers can dynamically store and manipulate objects of different types by allowing base class pointers to call derived class methods. This polymorphism makes STL very powerful in developing scalable and maintainable code.

The above is the detailed content of The application of C++ virtual functions in STL: revealing the polymorphic nature of containers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template