有关于逻辑常量性的思考

在物理常量性和逻辑常量性中进行分析的时候,我们可以发现在逻辑常量性中,一个成员函数逻辑上是const,但它仍然需要改变某个成员的值。对于用户来说,一个函数看似没有改变对象的状态,然而他有可能更新了用户不能直接访问的一部分数据。这个通常称为逻辑常量性(个人感觉是const带来的特性)。

(1)对于一个const成员函数,这个函数并没有改变这个对象的状态,但是有可能在其中改变了其成员变量的值,这需要我们进行调测,因此:
        通过一点蛮力进行操作,利用const_cast强制的去掉const,这样我们可以的到一个对象,没有const的对象,可以对其中的函数值进行改变。

 

ContractedBlock.gif ExpandedBlockStart.gif 代码一
 
   
1 class Date_noconst
2 {
3   public :
4 Date_noconst();
5 void make_cache();
6 string get_cache() const ;
7
8   private :
9 string cache;
10 bool cached;
11 };
12
13
14 void Date_noconst::make_cache()
15 {
16 cache = " Have cached!! " ;
17 cached = true ;
18 };
19
20   string Date_noconst::get_cache() const
21 {
22 if ( ! cached)
23 {
24 Date_noconst * th = const_cast < Date_noconst *> ( this );
25 th -> make_cache();
26 th -> cached = true ;
27 }
28 return cache;
29 };
30
31 Date_noconst::Date_noconst()
32 {
33 cached = false ;
34 cache = " No cached!! " ;
35 }

 

 

(2)若是一个const函数来调用另外一个函数,若是没有进行const_cast去除const,那么另外一个函数必为const函数,否则的话会发生错误(因为这个函数不  为const的话,调用函数认为其为可改变值,这与我们的调用函数设置的const发生了冲突);若要在这另一个函数进行改变值的话,可以参考(1)

 

ContractedBlock.gif ExpandedBlockStart.gif 代码二
 
   
1 void Date::make_cache() const
2 {
3 Date * th = const_cast < Date *> ( this );
4 th -> cached = true ;
5 th -> cache = " Have cached!! " ;
6 };
7
8   string Date::get_cache() const
9 {
10 if ( ! cached)
11 {
12 make_cache();
13 }
14 return cache;
15 };
16

 

 

(3)对成员变量设置mutable,表示这个成员变量表示不可能是const,这样的成员变量根本不需要进行我们的强制转化,就可以进行改变值,直接调用,但是其中调用的函数还应该是const,否则还是应该进强制转化后再进行调用的。

 

ContractedBlock.gif ExpandedBlockStart.gif 代码三
 
   
1 class Date
2 {
3 public :
4 Date();
5 void make_cache() const ;
6 string get_cache() const ;
7 private :
8 /* mutable string cache;
9 mutable bool cached; */
10 string cache;
11 bool cached;
12 // data *c;
13 };
14
15 Date::Date()
16 {
17 // c = new data();
18 cache = " No cached!! " ;
19 cached = false ;
20 };
21
22 void Date::make_cache() const
23 {
24 Date * th = const_cast < Date *> ( this );
25 th -> cached = true ;
26 th -> cache = " Have cached!! " ;
27 };
28
29 string Date::get_cache() const
30 {
31 if ( ! cached)
32 {
33 make_cache();
34 }
35 return cache;
36 };
37

 

 

(4)对于成员变量还可以将其作为一个数据结构组合起来,然后使用就和设置了mutable一个样。

 

ContractedBlock.gif ExpandedBlockStart.gif 代码四
 
   
1 struct data
2 {
3 string cache;
4 bool cached;
5 };
6
7 class Date
8 {
9 public :
10 Date();
11 void make_cache() const ;
12 string get_cache() const ;
13 private :
14 data * c;
15 };
16
17 Date::Date()
18 {
19 c = new data();
20 c -> cache = " No cached!! " ;
21 c -> cached = false ;
22 };
23
24 void Date::make_cache() const
25 {
26 c -> cached = true ;
27 c -> cache = " Have cached!! " ;
28 };
29
30 string Date::get_cache() const
31 {
32 if ( ! c -> cached)
33 {
34 make_cache();
35 }
36 return c -> cache;
37 };
38

 

转载于:https://www.cnblogs.com/onlylittlegod/archive/2010/06/09/1754599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值