0

C++要点笔记(27)——析构函数(destructor)

Posted by nanxiao on 一月 18, 2016 in C++要点笔记 |

与构造函数(constructor)对应的是析构函数(destructor)。析构函数的命名是类名前面加上~,它也没有返回值,并且没有参数,因此一个类只能有一个析构函数。举例如下:

#include <iostream>
using namespace std;

class Foo {
public:
    ~Foo() {cout << "Foo destructor is called" <<endl;}
};
int main() {
    // your code goes here
    Foo f;
    return 0;
}

析构函数会在下面3种情况下被调用:

1) When you delete a pointer to an object
2) When the object goes out of scope
3) When the object belongs to a class whose destructor is being called

 

标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Copyright © 2013-2017 我的站点 All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.