effective c++ 条款3 use const whereever you can

 1 const 传达的意思应该是这个变量是常量不能更改

 2 const 在 * 左边表示数据是const,在右边表示指针是const

   //
   char greeting[] = "hello";

   char* p = greeting;

   //const *: const data
   //* const: const pointer
   char const *p1 = greeting;    // const data
   char * const p2 = greeting;   // const pointer
   const char * const p3 = greeting; // const data and pointer

   //not correct p1 is const data
   //(*p1)++;

   //correct p1 is const data
   p1++;

   //correct p2 is const pointer
   (*p2)++;

   //not correct p2 is const pointer
   //p2++;

   //not correct p3 is  const data and pointer
   //(*p3)++;

   //not correct p3 is  const data and pointer
   //p3++;

  3 

//1 const return value avoid the error like this
// if( a+b = c) {...}
//2 const parameter
// it can avoid unintentioned change of parameter inside the function
const Rational operator+ (const Rational& lhs, const Rational& rhs)
{
   Rational tmp;
   tmp.real = lhs.real + rhs.real;
   tmp.img = lhs.img + rhs.img;

   return tmp;
}

class Rational
{
   public:
      float real;
      float img;

      Rational():real(0),img(0){};

};

  4 const 成员函数

        声明方式

       作用

 

       ConstMemberFunc.h

//---------------------------------------------------------------------------

#ifndef ConstMemberFuncH
#define ConstMemberFuncH
//---------------------------------------------------------------------------

#include <cstddef>    //for std::size_t


//1 const member function is used to operate const object
// eg. we have a class Textblock and it has a operator[] which is const
// when we have a const Textblock, the operator[] const will be used

//2
// a  const member function can change static class member
// b  const member function can not change non-static class member
// c  use mutable decleared variable can be changed in const member function

//3 const member function can only call const member function
//                        can not call non-const member function

//4 const object can only call const member function
//  const object can not call non const member function
class TextBlock
{
   public:

     static int TextBlockStaticNum;

      char* text;

       //1 const member function is used to operate const object
       //  eg. we have a class Textblock and it has a operator[] which is const
       //  when we have a const Textblock, the operator[] const will be used
      const char& operator[](std::size_t position) const
      {
         return text[position];

        //validate 3
        //not correct
        //3 const member function can only call const function
        //                        can not call non-const  function
       // print(); is the non-const function of vector

      };

      char& operator[](std::size_t position)
      {
         //return text[position];

        //call const operator[] and remove constness for return
        //not recommended
         return const_cast<char&>(                                      // remove the const from the return of const []
                                   static_cast<const TextBlock&>(*this) // add const to *this
                                                             [position] // call const []
                                   );
      };


      //2
      // a  const member function can change static class member
      // b  const member function can not change non-static class member
      // c  use mutable decleared variable can be changed in const member function
      int nonStaticOrMutable;
      mutable int textLen;
      const void constMemberFunctionTest(int Num) const
      {
         //a change static member
         TextBlockStaticNum = 1;

         //b
         //not correct
         //can not change non-static class member
         //nonStaticOrMutable = 1;

         //c  use mutable decleared variable can be changed in const member function
         textLen = 2;

      };

      //3 const member function can only call const member function
      //                        can not call non-const member function
       void print()
       {
         ;
       }


      TextBlock( char charArray[])
      {
          text = charArray;
          TextBlockStaticNum = 0;
          textLen = 0;
          nonStaticOrMutable=0;
      };
} ;

//1 const member function is used to operate const object
// eg. we have a class Textblock and it has a operator[] which is const
// when we have a const Textblock, the operator[] const will be used
//Test function
extern void ConstAndNonConstObjCallConstMemberFunction();

//2
// a  const member function can change static class member
// b  const member function can not change non-static class member
// c  use mutable decleared variable can be changed in const member function
extern void ConstMemFunctionChangeClassMember();

//4 const object can only call const member function
//  const object can not call non const member function
extern void ConstObjCanNotCallNonConstMember(const TextBlock & ctb)
{
    //not correct
    //print is not a const member function
    //ctb.print();

    //correct
    const char t = ctb[0];

};

#endif

 ConstMemberFunc.cpp 

//---------------------------------------------------------------------------

#pragma hdrstop

#include "ConstMemberFunc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

#include "ConstMemberFunc.h"

void ConstAndNonConstObjCallConstMemberFunction()
{
   TextBlock tb("test");
   const TextBlock ctb("test");

   //call non const operator[]
   //correct
   tb[0] = '1';

   //call const operator[]
   //not correct, since ctb is const operator
   //ctb[0] = '1';
   //normally this is used as a function call
   //e.g. void print(const TextBlock ctb) //we do not change ctb in this function
}

void ConstMemFunctionChangeClassMember()
{
   TextBlock tb("test");
   tb.constMemberFunctionTest(5);


   const TextBlock ctb("test");
   ctb.constMemberFunctionTest(2);
}

  

       

 

       

转载于:https://www.cnblogs.com/williamwood/p/3807288.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值