部分习题解答:
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不支持重载,所以是非法的