C++ 智能指针的使用

简介:  测试环境: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;
}

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

相关文章
|
17天前
|
C++
C++(十八)Smart Pointer 智能指针简介
智能指针是C++中用于管理动态分配内存的一种机制,通过自动释放不再使用的内存来防止内存泄漏。`auto_ptr`是早期的一种实现,但已被`shared_ptr`和`weak_ptr`取代。这些智能指针基于RAII(Resource Acquisition Is Initialization)原则,即资源获取即初始化。RAII确保对象在其生命周期结束时自动释放资源。通过重载`*`和`-&gt;`运算符,可以方便地访问和操作智能指针所指向的对象。
|
17天前
|
C++
C++(九)this指针
`this`指针是系统在创建对象时默认生成的,用于指向当前对象,便于使用。其特性包括:指向当前对象,适用于所有成员函数但不适用于初始化列表;作为隐含参数传递,不影响对象大小;类型为`ClassName* const`,指向不可变。`this`的作用在于避免参数与成员变量重名,并支持多重串联调用。例如,在`Stu`类中,通过`this-&gt;name`和`this-&gt;age`明确区分局部变量与成员变量,同时支持链式调用如`s.growUp().growUp()`。
|
29天前
|
存储 安全 C++
C++:指针引用普通变量适用场景
指针和引用都是C++提供的强大工具,它们在不同的场景下发挥着不可或缺的作用。了解两者的特点及适用场景,可以帮助开发者编写出更加高效、可读性更强的代码。在实际开发中,合理选择使用指针或引用是提高编程技巧的关键。
23 1
|
1月前
|
安全 NoSQL Redis
C++新特性-智能指针
C++新特性-智能指针
|
1月前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
1月前
|
存储 安全 编译器
C++入门 | auto关键字、范围for、指针空值nullptr
C++入门 | auto关键字、范围for、指针空值nullptr
49 4
|
2月前
|
存储 安全 C++
浅析C++的指针与引用
虽然指针和引用在C++中都用于间接数据访问,但它们各自拥有独特的特性和应用场景。选择使用指针还是引用,主要取决于程序的具体需求,如是否需要动态内存管理,是否希望变量可以重新指向其他对象等。理解这二者的区别,将有助于开发高效、安全的C++程序。
27 2
|
2月前
|
存储 安全 C++
浅析C++的指针与引用
虽然指针和引用在C++中都用于间接数据访问,但它们各自拥有独特的特性和应用场景。选择使用指针还是引用,主要取决于程序的具体需求,如是否需要动态内存管理,是否希望变量可以重新指向其他对象等。理解这二者的区别,将有助于开发高效、安全的C++程序。
23 3
|
1月前
|
存储 C++
c++学习笔记06 指针
C++指针的详细学习笔记06,涵盖了指针的定义、使用、内存占用、空指针和野指针的概念,以及指针与数组、函数的关系和使用技巧。
29 0
|
1月前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针