1. 程式人生 > >c++ primer 第十八章習題

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 替代全域性靜態變數時

練習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(){}
	
};