zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++ 智能指针的使用

C++智能 指针 使用
2023-09-27 14:24:59 时间
 测试环境:win7, vs2012  如果未安装boost,请参考:http://blog.csdn.net/alex_my/article/details/17630685  涉及智能指针:shared_ptr, weak_ptr, scoped_ptr, auto_ptr  其它:enable_shared_from_this  总调用函数: testSmartPointer

 测试环境:win7, vs2012

 如果未安装boost,请参考:http://blog.csdn.net/alex_my/article/details/17630685

 涉及智能指针:shared_ptr, weak_ptr, scoped_ptr, auto_ptr

 其它:enable_shared_from_this

 总调用函数: testSmartPointer()

 可以将其放在main()中运行。解释在代码中。

#include string 

#include vector 

#include iostream 

#include boost/scoped_ptr.hpp 

#include boost/shared_ptr.hpp 

#include boost/weak_ptr.hpp 

#include boost/enable_shared_from_this.hpp 

class Base

public:

 explicit Base(int a)

 : m_a(a)

 virtual ~Base()

 int GetA() const

 return m_a; 

private:

 int m_a;

class Derive : public Base

public:

 explicit Derive(int b)

 : Base(2 * b)

 , m_b(b)

 virtual ~Derive()

 int GetB() const

 return m_b; 

private:

 int m_b;

class EnableShared

public:

 EnableShared()

 : m_e(3)

 ~EnableShared() 

 std::cout "EnableShared Destruction execute" std::endl;

 void ShowE()

 boost::shared_ptr EnableShared p1(this);

 std::cout p1- m_e std::endl;

private:

 int m_e;

class EnableSharedEx : public boost::enable_shared_from_this EnableSharedEx 

public:

 EnableSharedEx()

 : m_e(3)

 ~EnableSharedEx() 

 std::cout "EnableSharedEx Destruction execute" std::endl;

 void ShowE()

 //boost::shared_ptr EnableSharedEx p1(this);

 boost::shared_ptr EnableSharedEx p1 = shared_from_this();

 std::cout p1- m_e std::endl;

private:

 int m_e;

static void testSharedPtr();

static void testEnableSharedFromthis();

static void testScopedPtr();

static void testAutoPtr();

void testSmartPointer()

 // ------------- shared_ptr -------------

 testSharedPtr();

 // ------------- enable_shared_from_this -------------

 testEnableSharedFromthis();

 // ------------- scoped_ptr -------------

 testScopedPtr();

 // ------------- auto_ptr -------------

 testAutoPtr();

 // ------------- summary -------------

 // 1 auto_ptr会转移所有权,使原拥有者失效

 // 2 shared_ptr比起auto_ptr,不会转移所有权,而是增加引用计数

 // 3 scoped_ptr不允许复制

 // 4 weak_ptr起了类似于观察者的作用,不会对拥有者造成影响

void testSharedPtr()

 // 1 使用

 boost::shared_ptr Base pa(new Base(2));

 std::cout "testSharedPtr" pa- GetA() std::endl;

 // 2 发生引用,此时pa2和pa指向同一个指针,观察计数器share_ptr::use_count_ 值从1变为2。

 boost::shared_ptr Base pa2 = pa;

 // 3 弱引用,计数器并仍然是2,不过weak_count_ 从1变成了2。

 boost::weak_ptr Base p3 = pa;

void testEnableSharedFromthis()

 // 1 应用举例

 boost::shared_ptr EnableShared pe(new EnableShared);

 //pe- ShowE();

 // 2 注释说明

 // 编译可以通过,但是析构函数会执行两次,造成程序崩溃

 // shared_ptr的一个缺点,无法从this指针构造,无法像testSharedPtr中的引用例子一样。

 // 3 解决办法 enable_shared_from_this,改写EnableShared为EnableSharedEx

 boost::shared_ptr EnableSharedEx pex(new EnableSharedEx);

 pex- ShowE();

void testScopedPtr()

 // 1 应用举例、

 boost::scoped_ptr Base pb(new Base(2));

 std::cout "testScopedPtr" pb- GetA() std::endl;

 // 2 引用,无法通过编译,原因:scope_ptr不允许复制

 // boost::scoped_ptr Base pb2 = pb;

void testAutoPtr()

 // 1 应用举例,与shared_ptr相似

 std::auto_ptr Base pa(new Base(2));

 std::cout "testAutoPtr: " pa- GetA() std::endl;

 // 2 发生引用,与shared_ptr不同的地方在于pa编程空指针了。

 std::auto_ptr Base pax = pa;

}

不懂它的时候,你觉的它是洪水猛兽。了解它的时候,会觉得它是那么的亲切。


C++11 智能指针之shared_ptr void 基于Alexa的全链路智能语音SDK基于C++实现了跨平台特性,跑通了Android、Mac、Linux等设备,在兼容iOS时发现iOS未提供音频采集和播放的C++接口,所以需要改造SDK,允许SDK初始化时注入外部的采集器和播放器实现类,同时SDK中的Android播放器是基于ffmpeg解码 + opensl实现,但是考虑到包体积的问题,准备也基于这个接口在外部实现基于Android硬件解码的播放器。
C++第十一节——单例模式 C++11 智能指针 异常 有关讲述 可以用同样的方式来实现,就是将构造函数私有化,然后让创建类的时候只能通过一个接口函数来实现,而在这个接口函数中我们将其创建在栈上。
从C语言到C++你必须学会的---动态内存和智能指针 不管你是C++初学者,还是想从C语言转变为C++,你都应该了解C++的动态内存和智能指针,今天我们就来看一下有关这两个方面的内容。 本文章内容篇幅较长,且干货满满,感兴趣的大家可以收藏+点赞,以后慢慢看!