首页 析构函数和虚函数的用法及作用(The use and function of destructor and virtual function)

析构函数和虚函数的用法及作用(The use and function of destructor and virtual function)

举报
开通vip

析构函数和虚函数的用法及作用(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 use and function of destructor and virtual function The destructor is used to release the pointer used in th...

析构函数和虚函数的用法及作用(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 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)用虚函数是为了方便让基类的指针能访问派生类(用为循环实现),而不是一定要有,不写虚函数也是一样可以,那样就得用类的作用域来限定是想访问是基类还是派生类的成员,虚析构也是一样的道理--想用指针来访问派生类对象,里面涉及到动态分配内存的话,就要用虚析构。
本文档为【析构函数和虚函数的用法及作用(The use and function of destructor and virtual function)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_721103
暂无简介~
格式:doc
大小:22KB
软件:Word
页数:7
分类:生活休闲
上传时间:2018-02-01
浏览量:36