c++ primer 第十八章习题

c++ primer 第十八章习题


练习18.1 (a) range_error (b range_error 会抛出临时对象指针

练习18.2 会销毁函数中创造的对象,其中指针p会导致内存泄漏

练习18.3 可以把指针p使用智能指针或者封装成有析构函数的类

练习18.4 调换顺序即可

练习18.5 try {... }catch(exception e) { cout << e.what()<<endl;}

练习18.6 (a throw new exceptionType();
(b throw exception();
(c throw 1;

练习18.9

class isbn_mismatch: public std::logic_error {
public:
    explicit isbn_mismatch(const std::string &s): std::logic_error(s) {}
    isbn_mismatch(const std::string& s, const std::string &lhs, const std::string& rhs):
    std::logic_error(s),left(lhs),right(rhs) {}
    const std::string left, right;
    };

Sales_data&
Sales_data::operator+=(const Sales_data& rhs) {
  if(isbn() != rhs.isbn())
      throw isbn_mismatch("wrong isbn",isbn(),rhs.isbn());
      units_sold += rhs.units_sold;
      revenue += rhs.revenue;
      return *this;
      }

练习18.10
不抛出异常会terminate

练习18.11
在catch里处理异常时候使用what,再抛出异常可能无法处理,而且可能会导致递归。

练习18.13 替代全局静态变量时

扫描二维码关注公众号,回复: 4029761 查看本文章

练习18.14mathLib::MatrixLib::matrix mathLib::MatrixLib::matrix::opeator*();

练习18.15 using指示是声明将一个命名空间中的所有名字在当前作用域可见,一般看作是在最近的外层空间。允许名字冲突,调用时注明即可。

using声明是声明命名空间中某个名字在当前作用域可见。会隐藏外层作用域的变量。同名会冲突。

练习18.16位置1:ivar 使用声明时编译时冲突 使用指示时在调用时冲突
位置2:dvar 使用声明时重定义 使用指示ivar有调用二义性

练习18.18 string时使用string的作用域里,之后搜索std里
int时直接使用std里的。

练习18.19 只会使用std里的

练习18.20

namespace p  
{  
    void compute();//不可行  
    void compute(const void *);//可行,0->NULL  
}  
using p::compute;  
void compute(int);//可行,最佳匹配  
void compute(double, double = 1.1);//可行,int->double  
void compute(char*, char* = 0);//可行,0->NULL  
  
void f()  
{  
    compute(0);//与compute(int)版本最佳匹配  
}  
--------------------- 
作者:MISAYAONE 
namespace p{  
    void compute();//不可行,可见  
    void compute(const void *);//可行,0->NULL,可见  
}  
void compute(int);//可行,不可见,被隐藏
void compute(double, double = 1.1);//可行,int->double,被隐藏 
void compute(char*, char* = 0);//可行,0->NULL,被隐藏
void f(){  
    using p::compute;  
    compute(0);
}  
--------------------- 
作者:MISAYAONE 

练习18.21 (a 缺一个访问说明符,但可以 (b 重复继承 (c 正确

练习18.22 ABCXYZ MI

练习18.23 都可以

练习18.24 (1 Panda::print (2 失败 (3 失败 (4 失败 (5 可以

练习18.25 (a MI (b MI (c MI (d MI D2 B2 D1 B1 (e(f相同

练习18.26 二义性调用 在MI里重新定义一个

练习18.27 (a ival dval cval fval sval print
(b dval ival cval print
(c(d(e

void foo(double cval)  
{  
    int dval;  
    dval = Base1::dval + Derived::dval;//c  
    Base2::fval = dvec.back();//d  
    Derived::sval[0] = Base1::cval;//e
}  

练习18.28
直接访问: ival var
限定符: foo cval

练习18.29 FTFT

练习18.30

class Class
{
public:
	Class();
};

class Base: public Class
{
protected:
	int ival;
public:
	Base():ival(0){};
	Base(const Base& b) = default;
	Base(int a):Class(),ival(a){}
	
};

class D1: virtual public Base
{
public:
	D1():Base(){};
	D1(const D1& d)=default;
	D1(int a):Base(a){}
	
};

class D2: virtual public Base
{
public:
	D2():Base(){};
	D2(const D2& d)=default;
	D2(int a):Base(a){}
	
};

class MI: public D1,public D2
{
public:
	MI(){};
	MI(const MI& d):Base(d),D1(d),D2(d){};
	MI(int a):Base(a),D1(a),D2(a){}
	
};

class Final: public MI,public Class
{
public:
	Final(){};
	Final(const Final& d):Base(d),MI(d),Class(){};
	Final(int a):Base(a),MI(a),Class(){}
	
};

猜你喜欢

转载自blog.csdn.net/qq_25037903/article/details/83547528
今日推荐