関数の後ろにconstの役割を追加


メンバー関数を宣言するときにconstキーを使用すると、この関数が「読み取り専用(read-only)」関数であることを説明します.つまり、この関数がデータメンバー(object)を変更しないことを説明します.constメンバー関数を宣言するには、constキーワードを関数カッコの後ろに置きます.宣言も定義もconstキーワードを置くべきです.
データ・メンバーを変更しない関数はconstタイプとして宣言する必要があります.コンパイラは、constメンバー関数を作成するときにデータ・メンバーを誤って変更したり、他の非constメンバー関数を呼び出したりすると、エラーを指摘し、プログラムの堅牢性を向上させるに違いありません.
#include  class temp { public: temp(int age); int getAge() const; void setNum(int num); private: int age; };
temp::temp(int age) { this->age = age; }
int temp::getAge() const{age+=10;//#Error...error C2166: l-value specifies const object #return age; }
void main() { temp a(22); cout << "age= "<< a.getAge() << endl; }
const関数が宣言されているため、データメンバーを変更することはできませんが、ここでageデータメンバーに10を追加したため、エラーが発生しました.
      constthis             const  thisthisconstthis         const       constthisthis
 

ConstConstConstConst

  constC++           const  。           。      const    ,               。const                、   ,        。
const  constant   ,“    ”   。 const            ,         ,         。    C++        :“Useconst whenever you need”

     1. const       

        ,          ,      “    ”  “    ”,    const  ,            。const
        “    ”,   const              ,      。
  StringCopy
void StringCopy(char*strDestination, const char *strSource);
  strSourcestrDestinationstrSource  const             strSource   ,        。
        “   ”,                    ,            ,     const
       voidFunc1(int x)   voidFunc1(const int x)voidFunc2(A a)   voidFunc2(const A a)A
voidFunc(A a) A             a,        、  、          。
      ,         voidFunc(A &a),  “    ”            ,         。    voidFunc(A &a)       :
a,        。         , constvoidFunc(const A &a)
voidFunc(int x)    voidFunc(const int&x),      ?      ,                、     ,       ,“   ” “    ”       。
        ,    “const&”
              ,   “   ”     “const    ”,       voidFunc(A a)   voidFunc(const A &a)
             ,   “   ”     “const    ”。             ,           。  voidFunc(int x)      voidFunc(const int &x)

     2 const        

    “    ”         const  ,       (   )        ,          const        。    
constchar * GetString(void);

char*str = GetString();
      
constchar *str =GetString();
         “     ”,                      , const
       intGetInt(void)   constint GetInt(void)
       AGetA(void)   constA GetA(void)A
AGetA(void)    constA &GetA(void)       。          ,                   “  ”     “  ”    ,       。
       “    ”      ,                 ,           。

classA
{
A & operate = (const A &other); //     
};
Aa, b, c; // a, b, c  A   

a= b = c; //        
(a= b) = c; // 
            const  ,               。   ,  a= b = c (a= b) = c 

3const     

                   const     const     ,         ,        const    ,        ,            。     , stack     GetCountGetCount   constGetCount
classStack
{
public:
void Push(int elem);
int Pop(void);
intGetCount(void) const; // const     
private:
intm_num;
int m_data[100];
};
int Stack::GetCount(void)const
{
++ m_num; // m_num
Pop();// const  
returnm_num;
}
const const              ,                。
  Const

a.const      const    ,  const             ,  const    .
b.const           ,  const                  .
c.const              ,        const  .     ,            ,    .
e.    mutable        ,                 ,     const           
 
 
  :const
--------------------------------------------------------------------------------
    
AcGePoint3dstartPoint() const;
const           
==>
    const   this      

classA{
public:
f(int);
};
  fA*const this,      int     
      ff(constint),        f  thisthisconst       ,         ,  this    constA *constthis
const  *this   ,   “               。       ”           ,      *this const