C++primer 7.5.6节练习

练习7.53

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 class Debug{
 8 public:
 9     constexpr Debug(bool b = true) : hw(b), io(b), other(b) { }
10     constexpr Debug(bool h, bool i, bool o) : hw(h), io(i), other(o) { }
11     constexpr bool any() { return hw || io || other; }
12     void set_io(bool b) { io = b;}
13     void set_hw(bool b) { hw = b;}
14     void set_other(bool b) { hw = b;}
15 private:
16     bool hw;
17     bool io;
18     bool other;
19 
20 }; 
21 
22 
23 int main()
24 {
25     constexpr Debug io_sub(false, true, false);
26 if(io_sub.any())
27   cerr << "print appropriate error messages" << endl;
28 constexpr Debug prod(false);
29 if(prod.any())
30   cerr << "print an error message" << endl;
31 
32     return 0;
33  } 

练习7.54

不应该,因为set_需要将需要的数据成员设置成别的值,因此,不能声明成constexpr

练习7.55

不是,因为string不是一个字面值常量类。

posted @ 2017-08-08 12:27  五月份小姐  阅读(499)  评论(0编辑  收藏  举报