auto_ptr
来源:互联网 发布:linux 计划任务 每秒 编辑:程序博客网 时间:2024/04/30 13:04
namespace std{
template <class Y>
struct auto_ptr_ref{};
template <class T>
class auto_ptr{
typedef T element_type;
explicit auto_ptr(T *ptr=0) thow();
auto_ptr(auto_ptr &) thow();
template <class U>
auto_ptr(auto_ptr<U> &) thow();
auto_ptr & operator=(auto_ptr &) thow();
template <class U>
auto_ptr & operator= (auto_ptr<U> &) throw();
~auto_ptr() throw();
T * get() const throw();
T & operator *() const throw();
T * operator->const throw();
T *release() throw();
void reset(T * ptr=0) throw();
public:
auto_ptr(auto_ptr_ref<T>) throw();
auto_ptr & operator=(auto_ptr_ref<T> rhs) throw();
template<class U> operator auto_ptr_ref<U> () throw();
template<class U> opetator auto_ptr<U>() throw();
};
}
explicit auto_ptr::auto_ptr(T *ptr) throw()
1)生成一个auto_ptr,并拥有ptr所指对象
2)这一操作完成后,*this成为ptr所指对象的唯一拥有者。不容许再有其他拥有者。
c++中的explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?
如果c++类的构造函数有一个参数,那么在编译的时候就会有一个缺省的转换操作:将该构造函数对应数据类型的数据转换为该类对象,如下面所示: 在上面的代码中编译器自动将整型转换为MyClass类对象,实际上等同于下面的操作: 上面的所有的操作即是所谓的"隐式转换"。
如果要避免这种自动转换的功能,我们该怎么做呢?嘿嘿这就是关键字explicit的作用了,将类的构造函数声明为"显示",也就是在声明构造函数的时候前面添加上explicit即可,这样就可以防止这种自动的转换操作,如果我们修改上面的MyClass类的构造函数为显示的,那么下面的代码就不能够编译通过了,如下所示:
template <class Y>
struct auto_ptr_ref{};
template <class T>
class auto_ptr{
typedef T element_type;
explicit auto_ptr(T *ptr=0) thow();
auto_ptr(auto_ptr &) thow();
template <class U>
auto_ptr(auto_ptr<U> &) thow();
auto_ptr & operator=(auto_ptr &) thow();
template <class U>
auto_ptr & operator= (auto_ptr<U> &) throw();
~auto_ptr() throw();
T * get() const throw();
T & operator *() const throw();
T * operator->const throw();
T *release() throw();
void reset(T * ptr=0) throw();
public:
auto_ptr(auto_ptr_ref<T>) throw();
auto_ptr & operator=(auto_ptr_ref<T> rhs) throw();
template<class U> operator auto_ptr_ref<U> () throw();
template<class U> opetator auto_ptr<U>() throw();
};
}
explicit auto_ptr::auto_ptr(T *ptr) throw()
1)生成一个auto_ptr,并拥有ptr所指对象
2)这一操作完成后,*this成为ptr所指对象的唯一拥有者。不容许再有其他拥有者。
c++中的explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?
如果c++类的构造函数有一个参数,那么在编译的时候就会有一个缺省的转换操作:将该构造函数对应数据类型的数据转换为该类对象,如下面所示:
class MyClass
{
public:
MyClass( int num );
}
....
MyClass obj = 10; //ok,convert int to MyClass
{
public:
MyClass( int num );
}
....
MyClass obj = 10; //ok,convert int to MyClass
MyClass temp(10);
MyClass obj = temp;
MyClass obj = temp;
如果要避免这种自动转换的功能,我们该怎么做呢?嘿嘿这就是关键字explicit的作用了,将类的构造函数声明为"显示",也就是在声明构造函数的时候前面添加上explicit即可,这样就可以防止这种自动的转换操作,如果我们修改上面的MyClass类的构造函数为显示的,那么下面的代码就不能够编译通过了,如下所示:
class MyClass
{
public:
explicit MyClass( int num );
}
....
MyClass obj = 10; //err,can't non-explict convert
{
public:
explicit MyClass( int num );
}
....
MyClass obj = 10; //err,can't non-explict convert
- auto_ptr:
- Auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- Auto_Ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- 写给保研后的自己
- 在Ring0下获取进程路径
- 再谈内存映射
- 不要“像马一样笑”
- [双语阅读]美国政府发布锻炼指南
- auto_ptr
- 求职必考虑的六个问题
- 异常6
- UPS电池容量检测方法
- Netsh (DOS设置IP地址)[]
- H.264/MPEG-4 Part 10 White Paper 译(五) Reconstruction Filter
- 【转载】一点学习的经验,希望对每一位朋友都有用
- 开博啦
- 实时检测键盘输入(在窗体级别处理键盘输入)