C++中的析构函数

59 篇文章 0 订阅
52 篇文章 1 订阅

目录

摘要

动态内存管理

关键点

总结


摘要

析构函数(Destructor)是类的一种特殊成员函数,它在对象生命周期结束时被自动调用,用于执行清理操作。用途包括释放动态分配的内存、关闭文件、释放资源等。析构函数的名称与类名相同,但前面加上波浪号(~),且不接受任何参数,也没有返回值。

当`main`函数结束时,`obj`对象的析构函数会被自动调用:

#include <iostream>

class MyClass {
public:
    MyClass() {
        std::cout << "Constructor called" << std::endl;
    }
    ~MyClass() {
        std::cout << "Destructor called" << std::endl;
    }
};

int main() {
    MyClass obj;
    return 0;
}

动态内存管理

析构函数通常用于释放在构造函数中分配的动态内存,以避免内存泄漏。

析构函数中使用`delete[]`来释放构造函数中使用`new`分配的内存:

#include <iostream>

class MyClass {
private:
    int* data;
public:
    MyClass(int size) {
        data = new int[size];
        std::cout << "Constructor called" << std::endl;
    }
    ~MyClass() {
        delete[] data;
        std::cout << "Destructor called" << std::endl;
    }
};

int main() {
    MyClass obj(10);
    return 0;
}

关键点

1. 析构函数的虚函数特性:

如果一个类有可能被继承,并且会通过基类指针删除派生类对象,那么基类的析构函数应该声明为虚函数。这确保了删除派生类对象时,会正确调用派生类的析构函数。如果基类的析构函数不是虚函数,那么通过基类指针删除派生类对象时,只会调用基类的析构函数,导致资源泄漏。

#include <iostream>

class Base {
public:
    Base() {
        std::cout << "Base Constructor called" << std::endl;
    }
    virtual ~Base() {
        std::cout << "Base Destructor called" << std::endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        std::cout << "Derived Constructor called" << std::endl;
    }
    ~Derived() {
        std::cout << "Derived Destructor called" << std::endl;
    }
};

int main() {
    Base* obj = new Derived();
    delete obj; // 调用派生类的析构函数
    return 0;
}

2. 避免析构函数中抛出异常:

析构函数中如果抛出异常的话,可能会导致程序崩溃。因为在堆栈展开(stack unwinding)过程中,如果析构函数再次抛出异常,会导致`std::terminate`被调用。因此,析构函数中应尽量避免抛出异常,即使可以通过`noexcept(false)`明确声明析构函数可能抛出异常。

#include <iostream>

class MyClass {
public:
    ~MyClass() noexcept(false) { // 明确声明可能抛出异常
        throw std::runtime_error("Exception in Destructor");
    }
};

int main() {
    try {
        MyClass obj;
    } catch (const std::exception& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    return 0;
}

3. 对象资源管理:

使用RAIIResource Acquisition Is Initialization)技术,将资源的获取和释放封装在对象的构造和析构函数中,确保资源的正确释放。通过RAII技术,确保在对象生命周期结束时自动关闭文件。

#include <iostream>
#include <fstream>

class FileWrapper {
private:
    std::ofstream file;
public:
    FileWrapper(const std::string& filename) {
        file.open(filename);
        if (!file.is_open()) {
            throw std::runtime_error("Failed to open file");
        }
    }
    ~FileWrapper() {
        if (file.is_open()) {
            file.close();
        }
    }
    void write(const std::string& data) {
        file << data;
    }
};

int main() {
    try {
        FileWrapper fw("example.txt");
        fw.write("Hello, World!");
    } catch (const std::exception& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    return 0;
}

4. 处理资源的共享和转移:

在C++中,使用智能指针(如`std::unique_ptr`和`std::shared_ptr` -- C++中的智能指针类别-CSDN博客)可以自动管理对象的生命周期来自动管理资源,减少手动内存管理的复杂性。

- 使用`std::unique_ptr`管理独占资源。

#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass() {
        std::cout << "Constructor called" << std::endl;
    }
    ~MyClass() {
        std::cout << "Destructor called" << std::endl;
    }
};

void uniquePtrExample() {
    std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>();
}

int main() {
    uniquePtrExample();
    return 0;
}

- 使用`std::shared_ptr`管理共享资源。

#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass() {
        std::cout << "Constructor called" << std::endl;
    }
    ~MyClass() {
        std::cout << "Destructor called" << std::endl;
    }
};

void sharedPtrExample() {
    std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>();
    {
        std::shared_ptr<MyClass> ptr2 = ptr1;
        std::cout << "Use count: " << ptr1.use_count() << std::endl;
    }
    std::cout << "Use count after scope: " << ptr1.use_count() << std::endl;
}

int main() {
    sharedPtrExample();
    return 0;
}

总结

1. 析构函数:用于释放资源和执行清理操作,在对象生命周期结束时自动调用。
2. 虚析构函数:在基类中定义虚析构函数,确保通过基类指针删除派生类对象时正确调用派生类的析构函数。
3. 避免异常:析构函数中应避免抛出异常,以防止程序崩溃。
4. RAII:通过RAII技术,将资源管理封装在对象的构造和析构函数中,确保资源的正确释放。
5. 智能指针:使用`std::unique_ptr`和`std::shared_ptr`等智能指针来自动管理对象的生命周期,减少内存管理的复杂性和错误风险。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉夢志昂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值