CAutoVectorPtr 类

此类使用向量 new 和 delete 运算符表示智能指针对象。

重要

无法在 Windows 运行时中执行的应用程序中使用此类及其成员。

语法

template<typename T>
class CAutoVectorPtr

参数

T
指针类型。

成员

公共构造函数

名称 描述
CAutoVectorPtr::CAutoVectorPtr 构造函数。
CAutoVectorPtr::~CAutoVectorPtr 析构函数。

公共方法

名称 描述
CAutoVectorPtr::Allocate 调用此方法以分配由 CAutoVectorPtr 指向的对象数组所需的内存。
CAutoVectorPtr::Attach 调用此方法以获取现有指针的所有权。
CAutoVectorPtr::Detach 调用此方法可释放指针的所有权。
CAutoVectorPtr::Free 调用此方法以删除由 CAutoVectorPtr 指向的对象。

公共运算符

“属性” 描述
CAutoVectorPtr::operator T * 强制转换运算符。
CAutoVectorPtr::operator = 赋值运算符。

公共数据成员

“属性” 描述
CAutoVectorPtr::m_p 指针数据成员变量。

备注

此类提供用于创建和管理智能指针的方法,通过自动释放超出范围的资源来帮助防止内存泄漏。 CAutoVectorPtr 类似于 CAutoPtr,唯一的区别是 CAutoVectorPtr 使用 vector new[]vector delete[] 分配和释放内存,而不是 C++ newdelete 运算符。 如果需要 CAutoVectorPtr 的集合类,请参阅 CAutoVectorPtrElementTraits

有关使用智能指针类的示例,请参阅 CAutoPtr

要求

标头:atlbase.h

CAutoVectorPtr::Allocate

调用此方法以分配由 CAutoVectorPtr 指向的对象数组所需的内存。

bool Allocate(size_t nElements) throw();

参数

nElements
数组中的 元素数。

返回值

如果成功分配内存,则返回 true;如果失败,则返回 failure。

备注

在调试版本中,如果 CAutoVectorPtr::m_p 成员变量当前指向现有值,则会发生断言失败;也就是说,它不等于 NULL。

CAutoVectorPtr::Attach

调用此方法以获取现有指针的所有权。

void Attach(T* p) throw();

参数

p
CAutoVectorPtr 对象将获取此指针的所有权。

备注

CAutoVectorPtr 对象获取指针的所有权时,它将在超出范围时自动删除指针和任何已分配的数据。 如果调用 CAutoVectorPtr::Detach,程序员将再次负责释放任何已分配的资源。

在调试版本中,如果 CAutoVectorPtr::m_p 成员变量当前指向现有值,则会发生断言失败;也就是说,它不等于 NULL。

CAutoVectorPtr::CAutoVectorPtr

构造函数。

CAutoVectorPtr() throw();
explicit CAutoVectorPtr(T* p) throw();
CAutoVectorPtr(CAutoVectorPtr<T>& p) throw();

参数

p
现有指针。

备注

CAutoVectorPtr 对象可以使用现有指针创建,在这种情况下,它将转移指针的所有权。

CAutoVectorPtr::~CAutoVectorPtr

析构函数。

~CAutoVectorPtr() throw();

备注

释放任何已分配的资源。 调用 CAutoVectorPtr::Free

CAutoVectorPtr::Detach

调用此方法可释放指针的所有权。

T* Detach() throw();

返回值

返回对象的副本。

注解

释放指针的所有权,将 CAutoVectorPtr::m_p 变量设置为 NULL,并返回指针的副本。 调用 Detach 后,由程序员来释放任何分配的资源,CAutoVectorPtr 对象之前通过该资源承担了责任。

CAutoVectorPtr::Free

调用此方法以删除由 CAutoVectorPtr 指向的对象。

void Free() throw();

备注

释放 CAutoVectorPtr 指向的对象,CAutoVectorPtr::m_p 成员变量设置为 NULL。

CAutoVectorPtr::m_p

指针数据成员变量。

T* m_p;

备注

此成员变量保存指针信息。

CAutoVectorPtr::operator =

赋值运算符。

CAutoVectorPtr<T>& operator= (CAutoVectorPtr<T>& p) throw();

参数

p
指针。

返回值

返回对 CAutoVectorPtr< T >的引用。

注解

赋值运算符将 CAutoVectorPtr 对象与任何当前指针分离,并将新指针 p 附加到其位置。

CAutoVectorPtr::operator T *

强制转换运算符。

operator T*() const throw();

注解

返回指向类模板中定义的对象数据类型的指针。

另请参阅

CAutoPtr 类
类概述