English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Discussion on the understanding of virtual, abstract methods, static methods, and static variables

Let's talk about the understanding of these easily confused words:

1c++The virtual keyword in the virtual method is mainly set as an identifier to prevent the same method of the superclass from being inherited repeatedly in inheritance.

2The difference between virtual and abstract keywords is that virtual methods can have specific implementations, and when a subclass inherits a superclass, if the method is not overridden, it can also use the method in the superclass.

  However, abstract methods, that is, abstract methods, do not have specific implementations, and the subclass needs to implement them. To put it in a metaphorical way, virtual methods are like a father who is a bit 'virtual' but still has some 'inheritance'. But if the father is an abstract method, the son will be a tragedy, with no inheritance at all, because his father's method is abstract, not 'real'...

In addition: A class containing abstract methods must be an abstract class, and an abstract class may not have abstract methods (but it cannot be instantiated).

By the way, there is also an interface that is purely for abstract things. It only contains constants and abstract methods.

3About static methods and static variables

Static methods belong to the class, and memory must be allocated for it. This space is always occupied by static methods, and the memory manager will not reclaim the storage space of static methods because they have not been called. Therefore, if all methods are declared as static methods, a large amount of memory space will be occupied, and finally the system will slow down. While ordinary member methods are called by objects, memory is not always allocated for them, only when they are called will storage space be allocated for them, and when they are not called, the storage space will be reclaimed by the memory manager, releasing unused space, and improving the system's running speed! Hope it helps the author!

Specifically search Baidu, in addition: The typical usage of static methods is the singleton pattern. In addition, based on the characteristics of static methods, I think methods that need to be frequently called are suitable for defining as static methods. In addition, there are also special uses such as singleton.

Static variables: The statements defined by static will only be executed once.

This brief discussion on the understanding of virtual, abstract methods, and static methods and variables is all the content that the editor shares with everyone. It is hoped that it can provide a reference for everyone, and everyone is also hoped to support the Yanaohao tutorial more.

You May Also Like