Vocabulary function

xiaoxiao2021-03-06  22

The compiler always calls the class member function based on the type. However, a derived pointer can be safely converted into a base class pointer. When this deletes a base class pointer, C does not care about a base class object or a derived class object, the call is the destructor of the base class, not derived. If you rely on the code of the sect of the sect of the designer, there is no heavy-duty episode function, then there will be resource leaks.

So the recommended way is to declare the destructor as a virtual function. If you use the MFC and based on COBJECT or its derive class, the MFC has made this thing for you; the destructor of CObject is a virtual function. Once a function is declared as a virtual function, no matter whether you add a Virtual modifier, it becomes a virtual function in all derived classes. But in order to understand, it is recommended to add a Virtual modifier. C does not directly use the false argument function as the default value because the overhead of the virtual function table and the compatibility of the type of C language. Objects with virtual functions are always at the beginning of the object or at the end of the end (depending on the compiler) contains an implicit virtual function table pointer member. For small classes such as MFC Capoint CPoint and CSIZE, add a pointer member to add a lot of memory, and make its memory representation and the base class Point and Size are inconsistent. If the memory representation of the two classes is consistent, you can sometimes safely use a class pointer or array as another class of pointers or arrays. If you declare a false preframe function without implementing it, the compiler usually reports an error when it is connected - just like other virtual functions that are not implemented, but there is no detection of this problem, this is The object is collapsed when it is runtime. The recommended scheme is to embed the implementation in the class declaration after the declaration of the destructor.

转载请注明原文地址:https://www.9cbs.com/read-65342.html

New Post(0)