最基本的智能指针需包含如下头文件
#include "ace/Auto_Ptr.h"
智能制造使用的目的是为了避免使用指针的复杂性
基本使用如下:
auto_ptr< 类型> 变量名 (类型实例或者指向类型实例的指针);如
auto_ptr< ACE_Reactor> reactor ( new ACE_Reactor ( impl ));
当使用指向类型实例的指针时,当变量脱离作用范围,会自动释放其拥有指针的类型实例,如:
ACE_Reactor_Impl * impl = new ACE_Msg_WFMO_Reactor ;
auto_ptr< ACE_Reactor_Impl> delete_impl ( impl );
要获得智能指针所拥有的指针,需调用智能指针的 get () 方法,如
reactor . get ()
ACE 智能指针比较:
auto_ptr :不支持复制和赋值语法
ACE_Strong_Bound_Ptr :可以存储对象,但它不能把它拥有的对象的所有权交给其他对象
ACE_Refcounted_Ptr :也可以存储对象,能把它拥有的对象的所有权交给其他对象,
当调用其 release () 方法时会把自身置为 0 ,并返回其拥有的指针
#include "ace/Auto_Ptr.h"
智能制造使用的目的是为了避免使用指针的复杂性
基本使用如下:
auto_ptr< 类型> 变量名 (类型实例或者指向类型实例的指针);如
auto_ptr< ACE_Reactor> reactor ( new ACE_Reactor ( impl ));
当使用指向类型实例的指针时,当变量脱离作用范围,会自动释放其拥有指针的类型实例,如:
ACE_Reactor_Impl * impl = new ACE_Msg_WFMO_Reactor ;
auto_ptr< ACE_Reactor_Impl> delete_impl ( impl );
要获得智能指针所拥有的指针,需调用智能指针的 get () 方法,如
reactor . get ()
ACE 智能指针比较:
auto_ptr :不支持复制和赋值语法
ACE_Strong_Bound_Ptr :可以存储对象,但它不能把它拥有的对象的所有权交给其他对象
ACE_Refcounted_Ptr :也可以存储对象,能把它拥有的对象的所有权交给其他对象,
当调用其 release () 方法时会把自身置为 0 ,并返回其拥有的指针