1. about dynamic memory allocation
(1) The difference between new and malloc
|
Difference |
New |
Malloc |
|
Whether it is a keyword |
keyword , which is part of C + +. Supported by all C + + compilers. |
Functions provided by the C library, note that the function, rather than the keyword, depends on the C library (Cstdlib) and may not be called in some system development (such as embedded). |
|
Allocation unit |
In the specific type of unit |
In bytes |
|
Memory initialization |
Can be initialized at the time of application |
The amount of memory is requested only as needed, but not initialized . |
|
Whether the call to trigger the constructor |
is triggered. Object can only be created using the new |
No, only the required memory space is allocated and therefore not suitable for object-oriented development |
(2) The difference between delete/free
|
Difference |
Delete |
Free |
|
Whether it is a keyword |
keyword, which is part of C + +. Supported by all C + + compilers. |
Functions provided by the C library. may not be called in some system development, such as embedded. |
|
Whether to trigger a call to destructor |
Will trigger. Object destruction can only be done using the delete |
No, only the previously allocated memory space is not suitable for object-oriented development |
The difference between new and malloc in the "Programming Experiment"
#include <iostream>#include<string>#include<cstdlib>//For malloc\free function. It also means that both are functions, not keywords! using namespacestd;classtest{int*MP; Public: Test () {cout<<"test::test ()"<<Endl; MP=New int( -); cout<<*MP <<Endl; } ~Test () {DeleteMP; cout<<"~test::test ()"<<Endl; }};intMain () {Test* PN =NewTest;//The constructor is calledtest* pm = (test*)malloc(sizeof(Test));//constructors are not called DeletePn//destructors are called. If you mix free here, it will cause a memory leak! Free(PM);//If the delete is misused here to free the space referred to by PM, it will be called at the same time//destructor , but because the space indicated by the MP pointer is unknown, there is a bug of accidental deletion. return 0;}
2. about virtual functions
(1) Constructor:
The ① constructor itself cannot be a virtual function because the virtual function table pointer is initialized correctly after the execution of the constructor is complete.
② No polymorphism occurs when other virtual functions are called in the constructor. Because the virtual function table pointer was not initialized correctly during the execution of the constructor. So when a virtual function is called, only the version of the function defined in this class is called.
(2) destructor:
The ① destructor itself can be a virtual function , and it is recommended that the destructor be declared as a virtual function when designing the class, especially the class to be the parent class.
When other virtual functions are called in the ② destructor, there is no polymorphism, because the virtual function table pointers have been destroyed during the destructor. So when a virtual function is called, only the version of the function defined in this class is called, that is, statically bound.
"Programming Experiment" constructs, destructors and virtual functions
#include <iostream>#include<string>using namespacestd;classbase{ Public: Base ()//can not be a virtual function, even if the addition of virtual;{cout<<"Base ()"<<Endl; Func (); //There is no polymorphism, only the version of the function in this class is called! } Virtual voidfunc () {cout<<"Base::func ()"<<Endl; } Virtual~base ()//can be a virtual function{func ();//There is no polymorphism, only the version of the function in this class is called! cout <<"~base ()"<<Endl; }};classDerived: Publicbase{ Public: Derived () {cout<<"Derived ()"<<Endl; Func (); //There is no polymorphism, only the version of the function in this class is called! } Virtual voidfunc () {cout<<"Derived::func ()"<<Endl; } ~Derived () {func ();//There is no polymorphism, only the version of the function in this class is called! cout <<"~derived ()"<<Endl; }};intMain () {Base* p =NewDerived ();//note is the parent class pointer, if this is directly declared as derived* p = new Derived ()//the delete p invokes the constructor of the subclass directly. But one of our purposes in this case is to demonstrate//The destructor is polymorphic, so it is declared as a pin of the parent class. //...cout <<Endl; DeleteP//Delete Invokes a destructor. From this line of code, if the destructor is not declared as a virtual function in the parent class,//Delete a pointer to a parent class, because of the static binding, of course, is called the parent class destructor, this time will cause//the destructor for derived is not called. Of course, if the destructor is declared as a virtual function in the parent class, depending on the polymorphic//principle, the destructor of the subclass is called, and because of the characteristics of the destructor, the parent class destructor is automatically called First , and then the subclass is called.//their own destructors to properly free up memory. return 0;}/*output Base () Base::func ()//note, does not occur polymorphic Derived () Derived::func ()//note, and does not occur polymorphic Derived::func ()//note, and does not occur polymorphic ~derived () Base::func ()//Note that there is no polymorphic ~base ()*/
3. about forced type conversions in inheritance
(1) dynamic_cast is a type conversion keyword related to inheritance
(2) dynamic_cast requires that a virtual function be in the related class
(3) between pointers (references) for direct or indirect inheritance relationships
|
|
Conversion between pointers |
Conversions between references |
|
Conversion success |
Get pointer to target type |
Get a reference to the target type |
|
Conversion failed |
Get a null pointer |
Get an exception operation Information |
(4) The compiler will check if the dynamic_cast is used correctly
(5) The result of type conversion can only be obtained at run time.
The use of the "programming Experiment" dynamic_cast
#include <iostream>#include<string>using namespacestd;classbase{ Public: Base () {cout<<"base::base ()"<<Endl; } Virtual~Base () {cout<<"base::~base ()"<<Endl; }};classDerived: Publicbase{ Public:};intMain () {Base* p =NewBase; //the child class pointer will be initialized with the parent class pointer (for demonstration purposes, the use of itself is incomplete!). )//Note that it is also a reminder that you can use dynamic_cast to determine whether a class is another method of the parent class. //the specific method base* P = dynamic_cast (base* p) (pderived), that is, whether the subclass can be converted to a parent class. //if the return value is not NULL, it indicates a parent-child relationship. otherwise not. derived* pd = dynamic_cast<derived*> (P);//Note that there must be a virtual function in the class referred to by P//in this case, the destructor is declared as a virtual function. if(PD! = NULL)//The conversion succeeded. {cout<<"PD ="<< PD <<Endl; } Else //conversion failed, return null pointer null{cout<<"Cast error!"<<Endl; } Deletep; return 0;}/*output Result base::base () Cast error! Base::~base ()*/
4. Summary
(1) New/delete triggers a call to a constructor or destructor
(2) A constructor cannot be a virtual function, and a destructor can be a virtual function.
(3) Both constructors and destructors cannot produce polymorphic behavior .
(4) dynamic_cast is a special conversion keyword associated with inheritance .
The 55th lesson Classical problem Analysis (four)