2026年C/C++智能指针详解[可运行源码]

C/C++智能指针详解[可运行源码]C 智能指针演示程序 博客来源 https blog csdn net weixin article details 演示 C 11 中的三种智能指针 1 shared ptr 共享指针 允许多个指针共享同一对象 2 unique ptr 独占指针 同一时刻只能有一个指针拥有对象 3 weak ptr 弱指针 不增加引用计数

大家好,我是讯享网,很高兴认识大家。这里提供最前沿的Ai技术和互联网信息。

/

  • C++智能指针演示程序
  • 博客来源:https://blog.csdn.net/weixin_/article/details/
  • 演示C++11中的三种智能指针:
  • 1. shared_ptr - 共享指针,允许多个指针共享同一对象
  • 2. unique_ptr - 独占指针,同一时刻只能有一个指针拥有对象
  • 3. weak_ptr - 弱指针,不增加引用计数,用于解决循环引用问题 */

#include #include // 智能指针头文件 #include

using namespace std;

// User类定义 class User { private:

string name; int id; 

public:

GPT plus 代充 只需 145// 构造函数 User(string userName = "默认用户", int userId = 0) : name(userName), id(userId) { cout << "构造函数被调用 - 创建用户: " << name << " (ID: " << id << ")" << endl; } // 析构函数 ~User() { cout << "析构函数被调用 - 销毁用户: " << name << " (ID: " << id << ")" << endl; } // 测试函数 void TestFun() { cout << "测试函数 - 用户: " << name << " (ID: " << id << ")" << endl; } // 设置用户信息 void SetInfo(string userName, int userId) { name = userName; id = userId; cout << "用户信息已更新: " << name << " (ID: " << id << ")" << endl; } // 获取用户信息 string GetInfo() const { return "用户: " + name + " (ID: " + to_string(id) + ")"; } 

};

// 类型重定义,简化代码 typedef shared_ptr SPUser; typedef unique_ptr UPUser; typedef weak_ptr WPUser;

// 演示shared_ptr的使用 void demo_shared_ptr()

// 演示unique_ptr的使用 void demo_unique_ptr()

p2->TestFun(); // 交换指针 cout << " 

交换指针…" << endl;

GPT plus 代充 只需 145UPUser p3(new User("孙七", 2002)); p2.swap(p3); cout << "交换后p2的信息: " << p2->GetInfo() << endl; cout << "交换后p3的信息: " << p3->GetInfo() << endl; // 释放指针 cout << " 

释放p2…" << endl;

User* rawPtr = p2.release(); if (p2 == nullptr) { cout << "p2已释放,现在为空指针" << endl; } cout << "原始指针信息: " << rawPtr->GetInfo() << endl; // 手动删除原始指针 delete rawPtr; cout << "unique_ptr演示结束,即将离开作用域..." << endl; 

}

// 演示weak_ptr的使用 void demo_weak_ptr()

GPT plus 代充 只需 145} // 重置shared_ptr cout << " 

重置shared_ptr…" << endl;

sp1.reset(); sp2.reset(); cout << "重置后引用计数: " << wp1.use_count() << endl; cout << "weak_ptr是否过期: " << (wp1.expired() ? "是" : "否") << endl; // 尝试从过期的weak_ptr创建shared_ptr SPUser sp4 = wp1.lock(); if (!sp4) { cout << "无法从过期的weak_ptr创建shared_ptr" << endl; } cout << "weak_ptr演示结束,即将离开作用域..." << endl; 

}

// 演示循环引用问题及weak_ptr的解决方案 void demo_circular_reference() {

GPT plus 代充 只需 145cout << " 

========== 循环引用问题演示 ==========" << endl;

// 定义两个互相引用的类 class NodeA; class NodeB; class NodeA { public: shared_ptr 
  
    
    
      b_ptr; string name; NodeA(string n) : name(n) { cout << "创建NodeA: " << name << endl; } ~NodeA() { cout << "销毁NodeA: " << name << endl; } }; class NodeB { public: shared_ptr 
     
       a_ptr; string name; NodeB(string n) : name(n) { cout << "创建NodeB: " << name << endl; } ~NodeB() { cout << "销毁NodeB: " << name << endl; } }; // 演示循环引用导致的内存泄漏 cout << " 
      
    
  1. 使用shared_ptr导致的循环引用(内存泄漏):" << endl; {
    GPT plus 代充 只需 145shared_ptr 
        
          
          
            a(new NodeA("节点A")); shared_ptr 
           
             b(new NodeB("节点B")); a->b_ptr = b; b->a_ptr = a; cout << "a引用计数: " << a.use_count() << endl; cout << "b引用计数: " << b.use_count() << endl; cout << "离开作用域,但由于循环引用,对象不会被销毁..." << endl; 
            
          

    } cout << "离开作用域后,对象应该被销毁,但由于循环引用,析构函数没有被调用!" << endl;

    // 使用weak_ptr解决循环引用 cout << "

  2. 使用weak_ptr解决循环引用:" << endl;

    class NodeC; class NodeD;

    class NodeC { public:

    weak_ptr 
        
          
          
            d_ptr; // 使用weak_ptr而不是shared_ptr string name; NodeC(string n) : name(n) { cout << "创建NodeC: " << name << endl; } ~NodeC() { cout << "销毁NodeC: " << name << endl; } 
          

    };

    class NodeD { public:

    GPT plus 代充 只需 145weak_ptr 
        
          
          
            c_ptr; // 使用weak_ptr而不是shared_ptr string name; NodeD(string n) : name(n) { cout << "创建NodeD: " << name << endl; } ~NodeD() { cout << "销毁NodeD: " << name << endl; } 
          

    };

    {

    shared_ptr 
        
          
          
            c(new NodeC("节点C")); shared_ptr 
           
             d(new NodeD("节点D")); c->d_ptr = d; d->c_ptr = c; cout << "c引用计数: " << c.use_count() << endl; cout << "d引用计数: " << d.use_count() << endl; cout << "离开作用域,对象会被正常销毁..." << endl; 
            
          

    } cout << "离开作用域后,对象被正常销毁!" << endl; }

int main() {

小讯
上一篇 2026-03-18 13:39
下一篇 2026-03-18 13:37

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/243352.html