関数の後ろにconstの役割を追加
26966 ワード
メンバー関数を宣言するときに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を追加したため、エラーが発生しました.
データ・メンバーを変更しない関数は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を追加したため、エラーが発生しました.
const ,this const 。 this , this 。
const ,this const const 。 this , this 。
:Const,Const ,Const , Const
const ,C++ const 。 。 const , 。const 、 , 。
const constant ,“ ” 。 const , , 。 C++ :“Useconst whenever you need”。
1. const
, , “ ” “ ”, const , 。const :
“ ”, const , 。
StringCopy :
void StringCopy(char*strDestination, const char *strSource);
strSource ,strDestination 。 strSource 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, 。 , const , voidFunc(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 GetCount , GetCount const 。 GetCount 。
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);
};
f , A*const this, int
f , f(constint), f this ? this ,const , , this constA *constthis。
const *this , “ 。 ” , *this const