class Simple {
public:
Simple(int param = 0) {
number = param;
std::cout << "Simple: " << number << std::endl;
}
~Simple() {
std::cout << "~Simple: " << number << std::endl;
}
void PrintSomething() {
std::cout << "PrintSomething: " << info_extend.c_str() << std::endl;
}
std::string info_extend;
int number;
};
void TestAutoPtr() {
std::auto_ptr<Simple> my_memory(new Simple(1)); // 创建对象,输出:Simple:1
if (my_memory.get()) { // 判断智能指针是否为空
my_memory->PrintSomething(); // 使用 operator-> 调用智能指针对象中的函数
my_memory.get()->info_extend = "Addition"; // 使用 get() 返回裸指针,然后给内部对象赋值
my_memory->PrintSomething(); // 再次打印,表明上述赋值成功
(*my_memory).info_extend += " other"; // 使用 operator* 返回智能指针内部对象,然后用“.”调用智能指针对象中的函数
my_memory->PrintSomething(); // 再次打印,表明上述赋值成功
}
} //my_memory栈对象即将结束生命期,析构堆对象Simple(1)
Simple: 1 PrintSomething: PrintSomething: Addition PrintSomething: Addition other ~Simple: 1
void TestAutoPtr2() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
std::auto_ptr<Simple> my_memory2; // 创建一个新的 my_memory2 对象
my_memory2 = my_memory; // 复制旧的 my_memory 给 my_memory2
my_memory2->PrintSomething(); // 输出信息,复制成功
my_memory->PrintSomething(); // 崩溃
}
}
void TestAutoPtr3() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory.release();
}
}
Simple: 1
void TestAutoPtr3() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
Simple* temp_memory = my_memory.release();
delete temp_memory;
}
}
void TestAutoPtr3() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory.reset(); // 释放 my_memory 内部管理的内存
}
}
void TestScopedPtr() {
boost::scoped_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory->PrintSomething();
my_memory.get()->info_extend = "Addition";
my_memory->PrintSomething();
(*my_memory).info_extend += " other";
my_memory->PrintSomething();
my_memory.release(); // 编译 error: scoped_ptr 没有 release 函数
std::auto_ptr<Simple> my_memory2;
my_memory2 = my_memory; // 编译 error: scoped_ptr 没有重载 operator=,不会导致所有权转移
}
}
void TestSharedPtr(boost::shared_ptr<Simple> memory) { // 注意:无需使用 reference (或 const reference)
memory->PrintSomething();
std::cout << "TestSharedPtr UseCount: " << memory.use_count() << std::endl;
}
void TestSharedPtr2() {
boost::shared_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory->PrintSomething();
my_memory.get()->info_extend = "Addition";
my_memory->PrintSomething();
(*my_memory).info_extend += " other";
my_memory->PrintSomething();
}
std::cout << "TestSharedPtr2 UseCount: " << my_memory.use_count() << std::endl;
TestSharedPtr(my_memory);
std::cout << "TestSharedPtr2 UseCount: " << my_memory.use_count() << std::endl;
//my_memory.release();// 编译 error: 同样,shared_ptr 也没有 release 函数
}
Simple: 1 PrintSomething: PrintSomething: Addition PrintSomething: Addition other TestSharedPtr2 UseCount: 1 PrintSomething: Addition other TestSharedPtr UseCount: 2 TestSharedPtr2 UseCount: 1 ~Simple: 1
void TestScopedArray() {
boost::scoped_array<Simple> my_memory(new Simple[2]); // 使用内存数组来初始化
if (my_memory.get()) {
my_memory[0].PrintSomething();
my_memory.get()[0].info_extend = "Addition";
my_memory[0].PrintSomething();
(*my_memory)[0].info_extend += " other"; // 编译 error,scoped_ptr 没有重载 operator*
my_memory[0].release(); // 同上,没有 release 函数
boost::scoped_array<Simple> my_memory2;
my_memory2 = my_memory; // 编译 error,同上,没有重载 operator=
}
}
void TestSharedArray(boost::shared_array<Simple> memory) { // 注意:无需使用 reference (或 const reference)
std::cout << "TestSharedArray UseCount: " << memory.use_count() << std::endl;
}
void TestSharedArray2() {
boost::shared_array<Simple> my_memory(new Simple[2]);
if (my_memory.get()) {
my_memory[0].PrintSomething();
my_memory.get()[0].info_extend = "Addition 00";
my_memory[0].PrintSomething();
my_memory[1].PrintSomething();
my_memory.get()[1].info_extend = "Addition 11";
my_memory[1].PrintSomething();
//(*my_memory)[0].info_extend += " other"; // 编译 error,scoped_ptr 没有重载 operator*
}
std::cout << "TestSharedArray2 UseCount: " << my_memory.use_count() << std::endl;
TestSharedArray(my_memory);
std::cout << "TestSharedArray2 UseCount: " << my_memory.use_count() << std::endl;
}
Simple: 0 Simple: 0 PrintSomething: PrintSomething: Addition 00 PrintSomething: PrintSomething: Addition 11 TestSharedArray2 UseCount: 1 TestSharedArray UseCount: 2 TestSharedArray2 UseCount: 1 ~Simple: 0 ~Simple: 0
void TestWeakPtr() {
boost::weak_ptr<Simple> my_memory_weak;
boost::shared_ptr<Simple> my_memory(new Simple(1));
std::cout << "TestWeakPtr boost::shared_ptr UseCount: " << my_memory.use_count() << std::endl;
my_memory_weak = my_memory;
std::cout << "TestWeakPtr boost::shared_ptr UseCount: " << my_memory.use_count() << std::endl;
}
Simple: 1 TestWeakPtr boost::shared_ptr UseCount: 1 TestWeakPtr boost::shared_ptr UseCount: 1 ~Simple: 1
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有