A brief discussion on virtual and abstract methods static methods and static variable understanding

  • 2020-06-12 09:01:15
  • OfStack

Here are some confusing words to understand:

1. The virtual keyword of virtual method in c++ is mainly used to prevent the duplicate inheritance of the same method of the parent class in inheritance.

2. The difference between the virtual and abstract keywords is that the virtual method can have a concrete implementation. When a subclass inherits from the parent class, it can also use the method in the parent class without overwriting the method.

But abstract methods, or abstract methods, have no concrete implementation, and subclasses need to implement them themselves. For example, virtual virtual method. Although the father is a little "virtual", he still has one point of "family property", but his father is abstract method. The son is tragic and has no inheritance, because his father's method is abstract, not "real"...

Also: Class 1 containing abstract methods must be an abstract class; an abstract class must not have abstract methods (but cannot be instantiated).

Well, there's also an interface for doing purely abstract things. There are only constants and abstract methods.

3. About static methods and static variables

A static method belongs to the class, the memory must be allocated memory space for it, 1 straight from static method to take up the space, the memory manager will not due to a static method is not called static method of storage space, so that if all the method statement as a static method, will take up a lot of memory space, and at last the system slower. And ordinary member method is called by the object, memory will not be 1 straight from the allocation of memory, only when it is called to allocate storage space, and when it is not called, storage space will be memory management its recovery, free up unused space, improve the running rate of the system! Hope to have help to the building Lord!

A typical use of static methods is the singleton pattern. In addition, based on the characteristics of static methods, I think those methods that need to be called frequently are suitable to be defined as static methods. In addition, there are singletons and other special purposes.

Static variables: Statements defined by static are executed only once.


Related articles: