const的思考[5]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 hustli 的 blog

                      mutable int test;   file://这里处理!
               };
           2)强制转换:const_cast
               class a
               {
               public:
                      a(int i=0):test(i)        { }
                      void setvalue(int i)const
                      { const_cast <int>(test)=i; }//这里处理!
               private:
                      int test;  
               };
            3)灵活的指针:int*
               class a
              {
               public:
                      a(int i=0):test(i)        { }
                      void setvalue(int i)const
                      { *test=i; }
               private:
                      int* test;   file://这里处理!
               };
            4)未定义的处理
              class a
              {
               public:
                      a(int i=0):test(i)        { }
                      void setvalue(int i)const
                      { int *p=(int*)&test; *p=i; }//这里处理!
               private:
                      int test;  
               };
                注意,这里虽然说可以这样修改,但结果是未定义的,避免使用!
             5)内部处理:this指针
              class a
              {
               public:
                      a(int i=0):test(i)        { }
                      void setvalue(int i)const

本文关键:const
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top