Problem:
Does the class require an argument-free constructor?
Does a class need a destructor?
Does the constructor of a class need to initialize all object members?
Does the class require a virtual destructor?
Some classes require virtual destructors just to declare that their destructors are virtual. A class that is never used as a base class does not require a virtual destructor: Any virtual function is useful only in the case of inheritance. Suppose B is a parent class, D is a subclass, and B needs a virtual destructor? There's only one willing to do it. The delete expression is executed on the b* pointer that actually points to the D-type object, and you need to add a virtual destructor to B, even if both B and D do not have virtual functions.
b* bp = new D;
Delete BP; If B does not have a virtual destructor, the wrong destructor is called
Does the class need to copy constructors?
The key is whether the object replicating the class is equivalent to replicating its data members and base class objects. If not, you need to copy the constructor. If your class allocates resources within the constructor, you might need a display copy constructor to manage the resources. If a member in a class is a pointer to an assigned resource, then a custom copy constructor is required, otherwise the implicit definition of the member will be copied, and the same memory will be released, and when two objects are destroyed, the same piece of memory is freed two times.
If you do not want users to be able to duplicate objects of a class, declare the copy constructor (and possibly the assignment operator) as private
Does your class need an assignment operator?
If you need to copy a constructor, you will probably need an assignment operator as well. If you do not want the user to be able to set objects in the class, the assignment operator is privatized. There is a special article about the assignment operator.
Remember to add a const to the parameter types of the copy constructor and assignment operator
In general, functions that do not have pointer members are not displayed with the Define assignment operator and assignment constructor.
We cannot use arrays to store objects to implement virtual mechanisms, which is not possible, and the most common solution is to use data to store pointers, not the objects themselves.
When do I need a virtual function:
If there is resource management in the parent class as a parent class, then the destructor must be a virtual function. If you do not discuss inheritance, then there is no need to talk about virtual functions, only in the event of an inheritance relationship will be inherited by the existence.
Remember that the difference between a virtual and a non-virtual function is only possible under a specific environment: when a base class pointer or reference is used to point to or reference a derived class object.
A virtual destructor is required when the next two things occur simultaneously:
There is a need to destructor for something to happen.
It occurs in a context where a pointer to a base class or a reference has a static type and actually points to an object of a derived class. So the virtual function is not necessary, it is not necessary, it needs to understand its own program to do what
In fact, for templates, it is often called static polymorphism. After the operator [] overload in the container, this operator returns to be a reference, otherwise how to modify this value?
Basic knowledge of C + +---constructors & destructors & virtual destructors