using namespace std;
//异常类
class CatException : public exception
{
public:
CatException(const string& errormsg) throw()
{
__errormsg = errormsg;
}
~CatException() throw()
{
}
const char* what() const throw()
{
return __errormsg.c_str();
}
private:
string __errormsg;
};
//
// 一个简单的自定义类型Cat
//
class Cat
{
public:
//构造
Cat (const string &name, int age)
{
if (name.empty())
{
throw CatException(\"Exception:猫咪姓名不能为空!\");
}
if (age<=0)
{
throw CatException(\"Exception:猫咪年龄只能为正!\");
}
__age = age;
__name = new char[name.length()+1];
strcpy(__name, name.c_str());
#ifdef NEEDDEBUG
cout << \"DEBUG:猫咪已被成功创建(年龄:\" << __age << \名字:\" << __name << \")\" << endl;
#endif
}
Cat (const char *name, int age)
{
if (strlen(name)==0)
{
throw CatException(\"Exception:猫咪姓名不能为空!\");
}
if (age<=0)
{
throw CatException(\"Exception:猫咪年龄只能为正!\");
}
__age = age;
__name = new char[strlen(name)+1];
strcpy(__name, name);
#ifdef NEEDDEBUG
cout << \"DEBUG:猫咪已被成功创建(年龄:\" << __age << \名字:\" << __name << \")\" << endl;
#endif
}
Cat (const Cat& ocat)
{
delete [] __name;
__name = new char[strlen(ocat.__name)+1];
strcpy(__name, ocat.__name);
#ifdef NEEDDEBUG
cout << \"DEBUG:猫咪已被成功创建(年龄:\" << __age << \名字:\" << __name << \")\" << endl;
#endif
}
//方法
void Beep() const
{
cout << \"猫咪\" << __name << \"叫了。}
int GetAge() const
{
return __age;
}
const
char *GetName() const
{
\" << endl;
return __name;
}
void SetName(string &name)
{
if (name.empty())
{
throw CatException(\"Exception:猫咪姓名不能为空!\");
}
strcpy(__name, name.c_str());
}
void SetName(char *name)
{
if (strlen(name)==0)
{
throw CatException(\"Exception:猫咪姓名不能为空!\");
}
strcpy(__name, name);
}
void SetAge(int age)
{
if (age<=0)
{
throw CatException(\"Exception:猫咪年龄只能为正!\");
}
__age = age;
}
//重载操作符
Cat &operator=(const Cat& ocat)
{
delete [] __name;
__name = new char[strlen(ocat.__name)+1];
strcpy(__name, ocat.__name);
return *this;
#ifdef NEEDDEBUG
cout << \"DEBUG:猫咪已被成功创建(年龄:\" << __age << \名字:\" << __name << \")\" << endl;
#endif
}
//析构
~Cat ()
{
#ifdef NEEDDEBUG
cout << \"DEBUG:猫咪将被删除(年龄:\" << __age << \名字:\" << __name << \")\" << endl;
#endif
delete [] __name;
}
private:
int __age;
char *__name;
};
int main(int argc, char *argv[])
{
try
{
auto_ptr mycatAptr(new Cat(\"Bother\//此时au_mycat_a便俘获了指向新建Cat对象的指针
//可以使用au_mycat_a来对对象进行操作了,比如“叫”
mycatAptr->Beep();
auto_ptr mycatBptr;mycatBptr = mycatAptr;
//此时,mycatAptr把控制权交给了mycatBptr
mycatBptr->Beep();
//此时如果再次调用au_mycat_a->Beep()就会产生错误。
mycatAptr.reset(mycatBptr.release());
//mycatAptr重启掌控对象,mycatBptr转让了对对象的控制
mycatAptr->Beep();
//此时如果mycatBptr->Beep()将会出错。
Cat *tmp = mycatAptr.release();
//mycatAptr也拒绝了对对象的控制,release()返回指向被放弃的对象的指针(对象并没有被删除)
tmp->Beep();
//此时mycatAptr已经不能在调用Beep()函数了。
}
catch (CatException &ce)
{
cout << ce.what() << endl;
}
return 0;
}
这是在mingw32-3.4.4下找到的源文件。
它被包含在std空间内。
//这个模板结构是为了让auto_ptr成为函数返回值
templatestruct auto_ptr_ref
{
_Tp1* _M_ptr;
explicit
auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
};
templateclass auto_ptr
{
private:
_Tp* _M_ptr;//真正存储的数据,一个指针型
public:
typedef _Tp element_type;//数据类型
//这种构造直接将指针赋值给_M_ptr
explicit
auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
//拷贝构造函数,会先将参数__a作废,然后使对象捕获__a原本指向的目标
auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
//带模板的转化函数,同上。
templateauto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
//这个=号赋值也会使__a作废,原因在此,注意。
auto_ptr&
operator=(auto_ptr& __a) throw()
{
reset(__a.release());
return *this;
}
//模板例程 ,效果同上。
templateauto_ptr&
operator=(auto_ptr<_Tp1>& __a) throw()
{
reset(__a.release());
return *this;
}
//当auto_ptr对象被销毁时,他无情的销毁了_M_ptr
~auto_ptr() { delete _M_ptr; }
//此处要注意防止_M_ptr为空!
element_type&
operator*() const throw()
{
_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
return *_M_ptr;
}
//同上
element_type*
operator->() const throw()
{
_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
return _M_ptr;
}
//返回_M_ptr,指针类型
element_type*
get() const throw() { return _M_ptr; }
//将对象对_M_ptr的控制拿掉,返回另一个对它的指向
element_type*
release() throw()
{
element_type* __tmp = _M_ptr;
_M_ptr = 0;//置0
return __tmp;
}
//如果__p和_M_ptr不一样的话,删除原_M_ptr内容,然后让其指向__p
void
reset(element_type* __p = 0) throw()
{
if (__p != _M_ptr)
{
delete _M_ptr;
_M_ptr = __p;
}
}
//为了匹配类似形式
//auto_ptr func_returning_auto_ptr(.....);auto_ptr(auto_ptr_ref __ref) throw(): _M_ptr(__ref._M_ptr) { }
//为了匹配类似形式
//auto_ptr ptr = func_returning_auto_ptr(.....);
auto_ptr&
operator=(auto_ptr_ref __ref) throw(){
if (__ref._M_ptr != this->get())
{
delete _M_ptr;
_M_ptr = __ref._M_ptr;
}
return *this;
}
//匹配两种转化形式
templateoperator auto_ptr_ref<_Tp1>() throw()
{ return auto_ptr_ref<_Tp1>(this->release()); }
templateoperator auto_ptr<_Tp1>() throw()
{ return auto_ptr<_Tp1>(this->release()); }