Http://zhidao.baidu.com/question/150072858.html
For common membersFunction virtual functions are called at runtime (that is, late binding, late binding.During compilation, you cannot know whether the object belongs to the class of the member function or the class derived from it ). virtual functions (udfs) are also called dynamic functions (dynamic) in some languages. virtual functions are called to determine the specific function. Objects must be created by Constructors (like nonsense). Therefore, constructors must have content and cannot be empty. The constructor cannot be used as a virtual function for the following reasons:
First, in terms of concept, the function of constructor is to convert an object into an existing object. In Any constructor, the object may only be partially formed-we can only know that the base class has been initialized, but we do not know which class is inherited from this base class. However,Virtual functions are called "Forward" and "outward.It can call functions in a derived class. If we do the same in the constructor, the function we call may not be initialized, which will lead to a disaster.
Second ,. When a constructor is called, one of its first tasks is to initialize its vptr. Therefore, it can only know that it is a "current" class, and ignore whether there is a successor behind this object. When the compiler generates code for this constructor, it generates code for the constructor of this class-neither the base class nor, it is not a derived class for it (because the class does not know who inherits it ). Therefore, the vptr used must be a vtable for this class. In addition, as long as it is the final constructor call, the vptr will be initialized to point to this vtable during the lifetime of this object. However, if another later-derived constructor is called, this constructor will set vptr to point to its vtable and wait until the end of the final constructor. The status of the vptr is determined by the final called constructor. This is another reason why constructor calls are ordered from the base class to a more derived class.However, when this series of constructor calls occur, each constructor has set vptr to point to its own vtable.If a function call uses a virtual mechanism, it will only generate a call through its own vtable, rather than the final vtable (the final vtable will be generated after all constructors are called ).
Therefore, constructors cannot be virtual.However, for destructor, it is often.