析构函数和虚函数的用法及作用(The use and function of destructor and virtual function)
析构函数和虚函数的用法及作用(The use and function of
destructor and virtual function)
The use and function of destructor and virtual function
The destructor is used to release the pointer used in the defined object, the default destructor does not display calls, and the self destructor needs to be called at the end of the program.
Virtual function can make the operation of the general membership function, object with a pointer to a derived class different when a pointer to call the virtual member function, it will call the real point to a member function object, and the definition of the member function in the base class (not as long as the derived class changed the member function). If it is not a virtual function, it calls the function defined in the base class, regardless of which derived class object the class pointer is pointing to.
If you define a member function as a virtual function, then the compiler adds a pointer (VPTR) to the beginning of the class and points to the virtual function table (VTBL). Each class has its own VTBL, VTBL's role is to save his address virtual function of the class, we can put the VTBL image as an array, each element of this array is stored in the virtual address. At run time, first take out the value of VPTR, which is the address of VTBL, and then go to VTBL here, and then call this function.
So, the same is true for virtual functions defined by destructor,
by finding the virtual function table of the actual object
through the pointer, and then invoking the virtual function of
the actual object.
What are the benefits of writing a class's destructor into a
virtual function?
Friday, October 10, 2008, 20:56
(1) interview questions:
Class Base
{
Public:
Base () {mPtr = New Int;}
~Base () {delete mPtr;}
Private:
Int* mPtr;
}
Class Derived: public Base
{
Public:
Derived () {mDerived = new long;}
~Derived () {delete mDerived;}
Private:
Long* mDerived;
}
Void, main ()
{
Base* P = new Derived;
Delete p;
}
Does the above program fragment generate memory leaks?
Two) answer:
If the class is polymorphic, be sure to write the destructor
as a virtual function
Class Base
{
Public:
Base () {mPtr = New Int;}
Virtual ~Base () {delete mPtr}
Private:
Int* mPtr;
};
Class Derived: public Base
{
Public:
Derived () {mDerived = new long;}
Virtual ~Derived () {delete mDerived}
Private:
Long* mDerived;
};
Three) other
(1) write virtual to avoid memory leaks when implementing
polymorphism, such as:
Class A
{
Int aa;
Public:
Virtual (~a) {};
};
Class B: public a
{
Int bb;
};
If you do:
A *pa = New B; / / upcast
And then:
Delete pa;
This delete, if your base class's destructor is not virtual, can cause memory leaks, specifically because the memory of the derived class is freed, and the base class does not
I've already given you references where the Efftive C++ is pretty good, and I'm not good at expressing it. When I use polymorphism in inheritance to create dynamic objects, such as: a *pa = new, b;
Because PA is a pointer to a base class, which belongs to the part of the base class can be identified, so if there is no virtual destructors, then the subclass specific part of it will not be released, resulting in "half release classics", leaked half of memory leaks.
This is the same principle as object slicing, as far as object slicing: is concerned
#include
#include
Using namespace std;
Class Pet
{
Public:
Pet (const string& _category_): category (_category_) {}
Virtual, void, Desc ()
{
Cout "This, is, a", "category", ".\n"";
}
Virtual, const, string&, GetCate ()
{
Return category;
}
Virtual (~Pet) {}
Private:
String category;
};
Class Cat: public Pet
{
Public:
Cat (const, string&, _category_, const, string&, _name_)
Pet (_category_), name (_name_) {}
Virtual, void, Desc ()
{
cout <<“这是一个“<<宠物::getcate() <<”。”;
cout <<“它的名字叫<<名<< endl;
}
私人:
字符串名称;
};
空描述(PET p)/对象切片
{
desc() P.;
}
国际main()
{
宠物P(黄色狗);
猫C(黑白猫),“基蒂”;
描述(P);
描述(c);/对象切片
}
所以表现在动态对象上就会造成删除不完全,造成内存泄露。
我的编译器警告级别被我调成最高,有一次写类多态的时候它就警告我基地类中没有虚的虚构函数,我开始也不懂为什么,但既然警告了就说明一定有问题,后来查了资料就知道了,自己也长了见识。一般的,只要一个类要作为其他类的基类,那么它就一定有虚函数,只要一个类中有虚函数,那么它的析构函数就一定也要是虚的,否则就会造成我以上所说的问题,你以后自己多看点
书
关于书的成语关于读书的排比句社区图书漂流公约怎么写关于读书的小报汉书pdf
查查资料吧„
参考资料:有效的C++项目7:宣布虚拟多态基类析构函数
(2)如果不改,则只会调用基类的析构函数。如果析构函数改为虚函数,则这个程序会先调用派生类的析构,后调用基类的析构。
(3)用虚函数是为了方便让基类的指针能访问派生类(用为循环实现),而不是一定要有,不写虚函数也是一样可以,那样就得用类的作用域来限定是想访问是基类还是派生类的成员,虚析构也是一样的道理--想用指针来访问派生类对象,里面涉及到动态分配内存的话,就要用虚析构。