[Effective C++]条款15:在资源管理类中提供对原始资源的访问

在C++中,使用RAII方式管理动态内存资源时,通常会将资源封装在一个类中。当需要获取类内部的原始资源时,可以通过以下几种方式实现:

1、显示转换函数

  • 通过提供一个显示的成员函数来获取原始资源。明确获取资源的意图。
class ResourceManager {
public:
    ResourceManager() : resource(new int(42)) {}
    ~ResourceManager() { delete resource; }

    // 显式获取原始资源
    int* get() const {
        return resource;
    }

private:
    int* resource;
};

int main() {
    ResourceManager rm;
    int* rawResource = rm.get();  // 显式获取资源
    std::cout << *rawResource << std::endl;  // 输出: 42
    return 0;
}

2、隐式转换函数

  • 通过重载函数转换运算符,使的资源管理类可以隐式转换为原始资源类型。这种方式虽然方便,但可能会引起意外的隐式转换。
class ResourceManager {
public:
    ResourceManager() : resource(new int(42)) {}
    ~ResourceManager() { delete resource; }

    // 隐式转换为原始资源类型
    operator int*() const {
        return resource;
    }

private:
    int* resource;
};

int main() {
    ResourceManager rm;
    int* rawResource = rm;  // 隐式转换为 int*
    std::cout << *rawResource << std::endl;  // 输出: 42
    return 0;
}

隐式转换函数获取原始资源,可能导致的问题

#include <iostream>

class ResourceManager {
public:
    ResourceManager() : resource(new int(42)) {}
    ~ResourceManager() { delete resource; }

    // 隐式转换为原始资源类型
    operator int*() const {
        return resource;
    }

private:
    int* resource;
};

void printValue(int* value) {
    std::cout << "Value: " << *value << std::endl;
}

int main() {
    ResourceManager rm;

    // 问题1:隐式转换导致意外的行为
    printValue(rm);  // 这里隐式调用了 operator int*()

    // 问题2:资源管理混乱
    int* rawResource = rm;  // 隐式转换为 int*
    delete rawResource;     // 错误:重复释放资源!

    // 问题3:代码可读性降低
    // 以下代码看起来像是在操作 ResourceManager,但实际上是在操作 int*
    *rm = 100;  // 这里隐式调用了 operator int*()
    std::cout << "Modified value: " << *rm << std::endl;

    return 0;
}

3、使用智能指针

  • 使用标准库提供的智能指针提供的get()方法获取原始资源
#include <memory>
#include <iostream>

int main() {
    std::unique_ptr<int> resource(new int(42));
    int* rawResource = resource.get();  // 获取原始资源
    std::cout << *rawResource << std::endl;  // 输出: 42
    return 0;
}

思维导图笔记:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值