c++ primer第五版----學習筆記(十九)Ⅱ
阿新 • • 發佈:2018-11-09
部分習題解答:
19.1、19.2:
#include <iostream> #include <cstdlib> using namespace std; void *operator new(size_t size) { cout << "new(size_t)" << endl; if (void *mem = malloc(size)) return mem; else throw bad_alloc(); } void operator delete(void *mem) noexcept { cout << "delete(void *)" << endl; free(mem); } int main() { int *a = new int(112); cout << a << " " << *a << endl; delete a; system("pause"); return 0; }
19.3、19.4:
#include <iostream> #include <cstdlib> #include <typeinfo> using namespace std; class A { public: A() { cout << "A()" << endl; } virtual ~A() { cout << "~A()" << endl; } }; class B : public A { public: B() { cout << "B()" << endl; } virtual ~B() { cout << "~B()" << endl; } }; class C : public B { public: C() { cout << "C()" << endl; } virtual ~C() { cout << "~C()" << endl; } }; class D : public B, public A { public: D() { cout << "D()" << endl; } virtual ~D() { cout << "~D()" << endl; } }; int main() { A *pa = new C; if (B *pb = dynamic_cast<B*>(pa)) { cout << "true" << endl; } else cout << "false" << endl; //19.4 try { C &pc = dynamic_cast<C&>(*pa);//pa指向C的物件,轉換成功 cout << "pc" << endl; }catch (bad_cast e){ cout << e.what() << endl; } delete pa; B *pb = new B; if (C *pc = dynamic_cast<C*>(pb)) { cout << "true" << endl; } else cout << "false" << endl; delete pb; A *pc = new D; if (B *pb = dynamic_cast<B*>(pc)) { cout << "true" << endl; } else cout << "false" << endl; delete pc; system("pause"); return 0; }
19.5:
想使用基類物件的指標或引用代替某個派生類操作或訪問某個派生類的成員而該成員基類沒有
19.6、19.7、19.8:
//19.6 Query_base *pb1 = new AndQuery(Query("value1"), Query("value2")); Query_base *pb2 = new OrQuery(Query("value1"), Query("value2")); if (AndQuery *pa1 = dynamic_cast<AndQuery*>(pb1)) { cout << "成功" << endl; } else { cout << "失敗" << endl; } if (AndQuery *pa2 = dynamic_cast<AndQuery*>(pb2)) { cout << "成功" << endl; } else { cout << "失敗" << endl; } //19.7 try { AndQuery &ra1 = dynamic_cast<AndQuery&>(*pb1); cout << "成功" << endl; } catch (bad_cast e) { cout << e.what() << endl; } try { AndQuery &ra2 = dynamic_cast<AndQuery&>(*pb2); cout << "成功" << endl; } catch (bad_cast e) { cout << e.what() << endl; } //19.8 if (typeid(*pb1) == typeid(*pb2)) cout << "pd1與pd2指向的物件型別相同" << endl; else cout << "pd1與pd2的動態型別不相同" << endl; if (typeid(*pb1) == typeid(AndQuery)) cout << "pd1的動態型別是AndQuery" << endl; else cout << "pd1的動態型別並非是AndQuery" << endl; if (typeid(*pb2) == typeid(AndQuery)) cout << "pd2的動態型別是AndQuery" << endl; else cout << "pd2的動態型別並非是AndQuery" << endl;
19.9:
#include <iostream>
#include <typeinfo>
#include <vector>
using namespace std;
int main()
{
int arr[10];
double i = 42;
vector<int> vec;
int *p = arr;
cout << typeid(arr).name() << endl;
cout << typeid(i).name() << endl;
cout << typeid(vec).name() << endl;
cout << typeid(p).name() << endl;
system("pause");
return 0;
}
19.10:
(a)class A* (b) class C (c) class B
19.11:
一般情況下,指標指向一個物件,但成員指標指示的是類的成員而非類的物件,成員指標還必須包含成員所屬䣌類
19.12:
static const pos Screen::data()
{
return &Screen::cursor;
}
const string::size_type Screen::*p = Screen::data();
19.13:
static const string Sales_data::data()
{
return &Sales_data::bookNo;
}
const string Sales_data::*p = Sales_data::data();
19.14:
合法,給指標重新賦值
19.15:
主要區別:在成員函式和指向該函式的指標之間不存在自動轉換規則
19.16:
using Avg = double(Sales_data::*)() const;
19.17:
using Action_c = char(Screen::*)() const;
using Acttion_get = char (Screen::*)(Screen::pos, Screen::pos) const;
using Action_mov = Screen& (Screeen::*)(pos, pos);
19.18:
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
vector<string> vec{ "csc","","safs","dsf","" };
function<bool(const string&)> p = &string::empty;
size_t _cnt = count_if(vec.begin(), vec.end(), p);
cout << "有" << _cnt << "個空字串" << endl;
system("pause");
return 0;
}
19.19:
vector<Sales_data>::const_iterator count(const vector<Sales_data> &vec, double d)
{
auto fun = bind(&Sales_data::avg_price, std::placeholders::_1);
return find_if(vec.cbegin(), vec.cend(), [&](const Sales_data &s) {return d < fun(s); }
}
19.21、19.22、19.23、19.25:
19.24:
賦給自己的每個類都會呼叫自身類所擁有的賦值建構函式
19.26:
兩個函式是過載的,而C不支援過載,所以是非法的