C++Primer第五版 7.5.6节练习

练习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;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值