练习 19.1:
#include<iostream>
#include<cstdlib>
using namespace std;
void* operator new(size_t size)
{
if (void* mem = malloc(size))
{
cout << "operator new" << endl;
return mem;
}
else
throw bad_alloc();
}
void operator delete(void* mem) noexcept
{
free(mem);
cout << "operator delete" << endl;
}
int main()
{
int* ip = new int(1);
delete ip;
return 0;
}
练习 19.3:
(b): 因为pb指向B对象,而目标转换类型是一个C(B的派生类)对象。
#include<iostream>
using namespace std;
class A {
public:
virtual ~A() {}
};
class B : public A{
public:
virtual ~B() {}
};
class C : public B {};
class D : public B, public A {};
int main()
{
A* pa = new C;
if (B* pb = dynamic_cast<B*>(pa))
cout << "转换成功" << endl;
else
cout << "转换失败" << endl;
B* pb = new B;
if (C* pc = dynamic_cast<C*>(pb))
cout << "转换成功" << endl;
else
cout << "转换失败" << endl;
A* pa2 = new D;
if (B* pb = dynamic_cast<B*>(pa2))
cout << "转换成功" << endl;
else
cout << "转换失败" << endl;
return 0;
}
练习 19.4:
#include<iostream>
using namespace std;
class A {
public:
virtual ~A() {}
};
class B : public A {
public:
virtual ~B() {}
};
class C : public B {};
class D : public B, public A {};
int main()
{
A* pa = new A;
A* pa2 = new C;
try
{
C& c = dynamic_cast<C&>(*pa);
cout << "dynamic_cast succeed" << endl;
}
catch (std::bad_cast& bc)
{
cout << "dynamic_cast failed" << endl;
}
try
{
C& c = dynamic_cast<C&>(*pa2);
cout << "dynamic_cast succeed" << endl;
}
catch (std::bad_cast& bc)
{
cout << "dynamic_cast failed" << endl;
}
return 0;
}
练习 19.5:
我们相使用基类对象的指针或引用执行某个派生类操作并且该操作不是虚函数。
练习 19.6 && 19.7 && 19.8:
#include<iostream>
using namespace std;
class Query_base
{
public:
Query_base(){}
virtual ~Query_base(){}
};
class BinaryQuery : public Query_base
{
};
class AndQuery : public BinaryQuery
{
};
int main()
{
Query_base* qptr = new AndQuery;
if (AndQuery* aptr = dynamic_cast<AndQuery*>(qptr))
cout << "dynamic_cast succeed for pointer" << endl;
else
cout << "dynamic_cast failed" << endl;
Query_base* qptr2 = new AndQuery;
try
{
AndQuery& a = dynamic_cast<AndQuery&>(*qptr2);
cout << "dynamic_cast succeed for reference" << endl;
}
catch (bad_cast& bc)
{
cout << "dynamic_cast failed for reference" << endl;
}
cout << boolalpha << (typeid(*qptr) == typeid(*qptr2)) << "\n"
<< (typeid(*qptr) == typeid(Query_base)) << endl;
}
练习 19.9:
#include<iostream>
#include<typeinfo>
using namespace std;
class Base{};
class Derived : public Base{};
class Sales_data{};
int main()
{
int arr[10];
Derived d;
Base* bptr = &d;
cout << typeid(42).name() << "\n"
<< typeid(3.14).name() << "\n"
<< typeid('c').name() << "\n"
<< typeid("cpp").name() << "\n"
<< typeid(true).name() << "\n"
<< typeid(arr).name() << "\n"
<< typeid(Sales_data).name() << "\n"
<< typeid(string).name() << "\n"
<< typeid(bptr).name() << "\n"
<< typeid(*bptr).name() << endl;
/*
int
double
char
char const [4]
bool
int [10]
class Sales_data
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
class Base *
class Base
*/
return 0;
}
练习 19.10:
#include<iostream>
#include<typeinfo>
using namespace std;
class A
{
public:
A(){}
virtual ~A(){}
};
class B : public A
{
public:
B(){}
virtual ~B(){}
};
class C : public B
{
public:
C() {}
virtual ~C() {}
};
int main()
{
A* pa = new C;
cout << typeid(pa).name() << endl; // class A *
C cobj;
A& ra = cobj;
cout << typeid(&ra).name() << endl; // class A*
B* px = new B;
A& ra2 = *px;
cout << typeid(ra2).name() << endl; // class B
return 0;
}
练习 19.11:
与普通指针不同的是,成员指针还必须包含成员所属的类。
练习 19.12:
#ifndef SCREEN_H
#define SCREEN_H
#include<iostream>
using namespace std;
class Screen
{
public:
typedef string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht* wd, ' ') {}
Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht* wd, c) {}
static const string Screen::* data() { return &Screen::contents; }
static const pos Screen::* cur() { return &Screen::cursor; }
char get() const
{
return contents[cursor];
}
char get(pos r, pos c) const
{
return contents[r * width + c];
}
inline Screen& move(pos r, pos c);
inline Screen& set(char ch);
inline Screen& set(pos r, pos c, char ch);
Screen& display(ostream& os)
{
do_display(os);
return *this;
}
const Screen& display(ostream& os)const
{
do_display(os);
return *this;
}
private:
void do_display(ostream& os)const
{
os << contents;
}
private:
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
inline Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
inline Screen& Screen::set(char ch)
{
contents[cursor] = ch;
return *this;
}
inline Screen& Screen::set(pos r, pos c, char ch)
{
cursor = r * width + c;
contents[cursor] = ch;
return *this;
}
#endif
#include"ex19_12.h"
int main()
{
Screen screen(3, 3, '*'), * pscreen = &screen;
screen.display(cout); // *********
cout << endl;
const string Screen::* pdata = Screen::data();
auto s = screen.*pdata;
cout << s << endl; // *********
screen.move(1, 0).set('c').display(cout); // ***c*****
cout << endl;
auto s2 = pscreen->*pdata;
cout << s2 << endl; // ***c*****
const Screen::pos Screen::* pcursor = Screen::cur();
auto c = screen.*pcursor;
cout << c << endl; // 3
screen.move(0, 0);
auto c2 = pscreen->*pcursor;
cout << c2 << endl; // 0
return 0;
}
练习 19.13:
#include<iostream>
#include<string>
using namespace std;
class Sales_data
{
public:
Sales_data() = default;
Sales_data(const string& s) : bookNo(s) {}
Sales_data(const string& s, unsigned n, double p)
: bookNo(s), units_sold(n), revenue(n* p) {}
string isbn()const { return bookNo; }
static const string Sales_data::* pbookNo() { return &Sales_data::bookNo; }
private:
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sales_data item("cpp", 6, 15.0);
const string Sales_data::* pbookNo = Sales_data::pbookNo();
string s = item.*pbookNo;
cout << s << endl;
return 0;
}
练习 19.14:
auto pmf = &Screen::get_cursor; //合法,指向Screen的get_cursor成员函数的指针
pmf = &Screen::get; //合法,指向Screen中无参数版本的get成员函数
练习 19.15:
指向成员函数的指针,要给出成员函数本身的类型,还需要给出成员函数所属类的类型,并指明成员函数是否为const。
练习 19.16:
using AvgPrice = double (Sales_data::*) const;
AvgPrice pavg = &Sales_data::avg_price;
练习 19.17:
using pmf1 = char (Screen::*)() const;
using pmf2 = char (Screen::*)(Screen::pos, Screen::pos) const;
using pmf3 = Screen& (Screen::*)(Screen::pos, Screen::pos);
using pmf4 = Screen& (Screen::*)(char);
using pmf5 = Screen& (Screen::*)(Screen::pos, Screen::pos, char);
using pmf6 = Screen& (Screen::*)(ostream&);
using pmf7 = const Screen& (Screen::*)(ostream&) const;
练习 19.18:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
// 使用mem_fn
size_t EmptyStringCnt(const vector<string>& svec)
{
auto ret = count_if(svec.begin(), svec.end(), mem_fn(&string::empty));
return ret;
}
int main()
{
vector<string> svec;
string s;
while (getline(cin, s))
svec.push_back(s);
for_each(svec.begin(), svec.end(),
[](const string& s) {cout << s << endl; });
// 使用lambda
auto cnt = count_if(svec.begin(), svec.end(),
[](const string& s) {return s.empty(); });
cout << cnt << endl;
cout << EmptyStringCnt(svec) << endl;
return 0;
}
练习 19.19:
#include<iostream>
#include"Sales_data.h"
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
using namespace placeholders;
// 使用lambda
//vector<Sales_data>::iterator find_first_greater_price(vector<Sales_data>& sv, double price)
//{
// auto it = find_if(sv.begin(), sv.end(),
// [price](const Sales_data& item) {return item.avg_price() > price; });
// return it;
//}
// 使用bind
bool high_price(Sales_data& item, double price)
{
return item.avg_price() > price;
}
vector<Sales_data>::iterator find_first_greater_price(vector<Sales_data>& sv, double price)
{
auto it = find_if(sv.begin(), sv.end(), bind(&high_price, _1, price));
return it;
}
int main()
{
vector<Sales_data> sv;
Sales_data item;
while (cin >> item)
sv.push_back(item);
cout << *find_first_greater_price(sv, 20.0) << endl;
return 0;
}
练习 19.20:
#ifndef TEXTQUERY_H
#define TEXTQUERY_H
#include<iostream>
#include<fstream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<memory>
using namespace std;
class TextQuery
{
public:
class QueryResult;
using line_no = vector<string>::size_type;
TextQuery(ifstream& is);
QueryResult query(const string& sought) const;
private:
shared_ptr<vector<string>> file; // 输入文件
// 每个单词到它所在的行号的集合的映射
map < string, shared_ptr<set<line_no>>> wm;
};
class TextQuery::QueryResult
{
friend ostream& print(ostream& os, const QueryResult& qr);
public:
QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) :
sought(s), lines(p), file(f) {}
private:
string sought; // 查询单词
shared_ptr<set<line_no>> lines; // 出现的行号
shared_ptr<vector<string>> file; // 输入文件
};
// 打印结果
ostream& print(ostream&, const TextQuery::QueryResult&);
#endif
#include"ex19_20.h"
#include<sstream>
TextQuery::TextQuery(ifstream& is) : file(new vector<string>)
{
string text;
while (getline(is, text))
{
file->push_back(text); // 保存此行文本
int n = file->size() - 1; // 当前行号
istringstream line(text); // 将行文本分解为单词
string word;
while (line >> word)
{
// 如果单词不在wm中,以之为下标在wm中添加一项
shared_ptr<set<line_no>>& lines = wm[word];
if (!lines) // 在我们第一次遇到这个单词时,此指针为空
lines.reset(new set<line_no>); // 分配一个新的set
lines->insert(n); // 将此行号插入set中
}
}
}
TextQuery::QueryResult TextQuery::query(const string& sought) const
{
// 如果未找到sought,我们将返回一个指向此set的指针
static shared_ptr<set<line_no>> nodata(new set<line_no>);
auto loc = wm.find(sought);
if (loc == wm.end())
return QueryResult(sought, nodata, file); // 未找到
else
return QueryResult(sought, loc->second, file);
}
// 打印结果
ostream& print(ostream& os, const TextQuery::QueryResult& qr)
{
// 如果找到了单词,打印出现次数和所有出现的位置
os << qr.sought << " occurs " << qr.lines->size() << (qr.lines->size() > 1 ? " times" : " time") << endl;
// 打印单词出现的每一行
for (auto num : *qr.lines)
os << "\t(line " << num + 1 << ") " << *(qr.file->begin() + num) << endl;
return os;
}
#include"ex19_20.h"
#include<sstream>
void runQueries(ifstream& infile)
{
// infile是一个ifstream,指向我们要处理的文件
TextQuery tq(infile); // 保存文件并建立查询map
// 与用户交互:提示用户输入要查询的单词,完成查询并打印结果
while (true)
{
cout << "enter word to look for, or q to quit: ";
string s;
// 若遇到文件尾或输入‘q’时循环终止
if (!(cin >> s) || s == "q")break;
// 指向查询并打印结果
print(cout, tq.query(s)) << endl;
}
}
int main()
{
ifstream ifs("storyDataFile.txt");
runQueries(ifs);
return 0;
}
练习 19.21:
#include<iostream>
using namespace std;
class Token
{
public:
// 因为union含有一个string成员,所以Token必须定义拷贝控制成员
Token() : tok(INT), ival{ 0 } { }
Token(const Token& t) : tok(t.tok) { copyUnion(t); }
Token& operator=(const Token&);
// 如果union有一个string成员,则我们必须销毁它
~Token() { if (tok == STR)sval.~string(); }
// 下面的赋值运算符设置union的不同成员
Token& operator=(const string&);
Token& operator=(char);
Token& operator=(int);
Token& operator=(double);
private:
enum { INT, CHAR, DBL, STR } tok; // 判别式
union
{
char cval;
int ival;
double dval;
string sval;
}; // 每个Token对象含有一个该未命名union类型的未命名对象
// 检查判别式,然后酌情拷贝union成员
void copyUnion(const Token&);
};
练习 19.22:
和string类型的成员同样的处理方式
练习 19.23:
#ifndef TOKEN_H
#define TOKEN_H
#include<iostream>
using namespace std;
class Token
{
friend std::ostream& operator<<(std::ostream&, const Token&);
public:
// 因为union含有一个string成员,所以Token必须定义拷贝控制成员
Token() : tok(INT), ival{ 0 } { }
Token(const Token& t) : tok(t.tok) { copyUnion(t); }
Token& operator=(const Token&);
Token(Token&& t) : tok(std::move(t.tok)) { moveUnion(std::forward<Token>(t)); }
Token& operator=(Token&& t);
// 如果union有一个string成员,则我们必须销毁它
~Token() { if (tok == STR)sval.~string(); }
// 下面的赋值运算符设置union的不同成员
Token& operator=(const string&);
Token& operator=(char);
Token& operator=(int);
Token& operator=(double);
private:
enum { INT, CHAR, DBL, STR } tok; // 判别式
union
{
char cval;
int ival;
double dval;
string sval;
}; // 每个Token对象含有一个该未命名union类型的未命名对象
// 检查判别式,然后酌情拷贝union成员
void copyUnion(const Token&);
void moveUnion(Token&&);
};
#endif
#include"ex19_21.h"
Token& Token::operator=(const string& s)
{
if (tok == STR) // 如果当前储存的是string,可以直接赋值
sval = s;
else
new(&sval) string(s); // 否则需要先构造一个string
tok = STR;
return *this;
}
Token& Token::operator=(char c)
{
if (tok == STR) sval.~string();
cval = c;
tok = CHAR;
return *this;
}
Token& Token::operator=(int i)
{
if (tok == STR) sval.~string(); // 如果当前存储的是string,释放它
ival = i;
tok = INT;
return *this;
}
Token& Token::operator=(double d)
{
if (tok == STR) sval.~string();
dval = d;
tok = DBL;
return *this;
}
void Token::copyUnion(const Token& t)
{
switch (t.tok)
{
case Token::INT: ival = t.ival; break;
case Token::CHAR: cval = t.cval; break;
case Token::DBL: dval = t.dval; break;
case Token::STR: new(&sval) string(t.sval); break;
}
}
Token& Token::operator=(const Token& t)
{
// 如果此对象的值是string而t的值不是,我们必须释放原来的string
if (tok == STR && t.tok != STR)
sval.~string();
if (tok == STR && t.tok == STR)
sval = t.sval; // 无需构造一个新的string
else // 此对象的值不是string
copyUnion(t);
tok = t.tok;
return *this;
}
void Token::moveUnion(Token&& t)
{
switch (t.tok)
{
case Token::INT: ival = std::move(t.ival); break;
case Token::CHAR: cval = std::move(t.cval); break;
case Token::DBL: dval = std::move(t.dval); break;
case Token::STR: new(&sval) string(std::move(t.sval)); break;
}
}
Token& Token::operator=(Token&& t)
{
if (this != &t)
{
tok = std::move(t.tok);
moveUnion(std::forward<Token>(t));
}
return *this;
}
ostream& operator<<(ostream& os, const Token& t)
{
switch (t.tok)
{
case Token::INT: return os << t.ival;
case Token::CHAR: return os << t.cval;
case Token::DBL: return os << t.dval;
case Token::STR: return os << t.sval;
}
}
练习 19.24:
定义移动赋值运算符时考虑了自赋值的情况,避免不必要或者是灾难性的后果。
练习 19.25:
#include"Token.h"
int main()
{
Token token1;
Token token2;
cout << token1 << endl;
token1 = 'c';
cout << token1 << endl;
token1 = 3.14;
cout << token1 << endl;
token2 = "cpp";
cout << token2 << endl;
token1 = token2;
cout << token1 << endl;
return 0;
}
练习 19.26:
不合法,C语言不支持函数重载。