练习7.53:定义你自己的Debug。
答:见 练习7.53.cpp
练习7.54:Debug中以set_开头的成员应该被声明成constexpr吗?如果不,为什么?
答:不应该,因为set_需要将需要的数据成员设置成别的值,因此,不能声明成constexpr的
练习7.55:7.5.5节(第266页)的Data类是字面值常量吗?请解释原因。
答:不是,因为string不是一个字面值常量类。
练习7.53
/*
*练习7.53
*2015/7/20
*问题描述:练习7.53:定义你自己的Debug。
*功能:Debug
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Debug{
public:
constexpr Debug(bool b = true) : hw(b), io(b), other(b) { }
constexpr Debug(bool h, bool i, bool o) : hw(h), io(i), other(o) { }
constexpr bool any() { return hw || io || other; }
void set_io(bool b) { io = b;}
void set_hw(bool b) { hw = b;}
void set_other(bool b) { hw = b;}
private:
bool hw;
bool io;
bool other;
};
int main()
{
constexpr Debug io_sub(false, true, false);
if(io_sub.any())
cerr << "print appropriate error messages" << endl;
constexpr Debug prod(false);
if(prod.any())
cerr << "print an error message" << endl;
return 0;
}