载入中。。。 'S bLog
 
载入中。。。
 
载入中。。。
载入中。。。
载入中。。。
载入中。。。
载入中。。。
 
填写您的邮件地址,订阅我们的精彩内容:


 
VC++学习笔记9-异常的参数传递
[ 2010/7/17 12:18:00 | By: 梦翔儿 ]
 

//异常的参数传递
#i nclude <iostream>
#i nclude <string>
using namespace std;

class my_exception
{
public:
 my_exception(int i=0)
 {
  cout<<"######"<<endl;
  _i=i;
 }
 my_exception(const my_exception& obj)
 {
  _i=obj._i+1;
  cout<<'#'<<_i<<endl;
 }
 my_exception& operator=(const my_exception&obj)
 {
  if(this!=&obj)
  {
   _i=obj._i+1;
  }
  return *this;

 }
 void print()
 {
  cout<<'$'<<_i<<endl;
 }
 ~my_exception()
 {
  cout<<"-----"<<endl;
 }
private:
 int _i;
};

int main()
{
 try
 {
  throw my_exception(1);

 }
 catch(my_exception err)
 {
  err.print();
 }
 return 0;
 
}
//C++标准要求throw表达式初始化一个临时对象,这个对象一直存在,直到该异

常被完全处理了为止。
=========
//异常的参数传递
#i nclude <iostream>
#i nclude <string>
using namespace std;

class my_exception
{
public:
 my_exception(int i=0)
 {
  cout<<"######"<<endl;
  _i=i;
 }
 my_exception(const my_exception& obj)
 {
  _i=obj._i+1;
  cout<<'#'<<_i<<endl;
 }
 my_exception& operator=(const my_exception&obj)
 {
  if(this!=&obj)
  {
   _i=obj._i+1;
  }
  return *this;

 }
 void print()
 {
  cout<<'$'<<_i<<endl;
 }
 ~my_exception()
 {
  cout<<"-----"<<endl;
 }
private:
 int _i;
};

int main()
{
 my_exception ex(1);
 try
 {
  throw ex;

 }
 catch(my_exception err) //catch(my_exception& err) 可以避免后面

的一次创建对象的工作。
 {
  err.print();
 }
 return 0;
 
}
//C++标准要求throw表达式初始化一个临时对象,这个对象一直存在,直到该异

常被完全处理了为止。
========

 
 
发表评论:
载入中。。。

 
 
 

梦翔儿网站 梦飞翔的地方 http://www.dreamflier.net
中华人民共和国信息产业部TCP/IP系统 备案序号:辽ICP备09000550号

Powered by Oblog.