logo

C++ の Const メンバー関数

定数メンバー関数は、クラスのデータ メンバーの値を変更する権限を拒否された関数です。メンバー関数を定数にするには、キーワード const が関数プロトタイプと関数定義ヘッダーに追加されます。

メンバー関数やメンバー関数の引数と同様、クラスのオブジェクトも const として宣言できます。 const として宣言されたオブジェクトは変更できないため、オブジェクトを変更しないようにするために const メンバー関数のみを呼び出すことができます。 const オブジェクトは、オブジェクト宣言の先頭に const キーワードを付けることで作成できます。 const オブジェクトのデータ メンバーを変更しようとすると、コンパイル時エラーが発生します。



構文

const メンバー関数は、次の 3 つの方法で定義できます。

1. クラス内の関数宣言の場合。

 return_type function_name () const ;>

例:

25/100
int get_data() const;>

2. クラス宣言内の関数定義の場合。

 return_type  function_name ()  const { //function body }>

例:



int get_data() const { //function body }>

3. クラス外の関数定義の場合。

return_type class_name::function_name () const { //function body }>

例:

int Demo :: get_data() const { }>

注意事項

  • 関数が const として宣言されている場合、その関数は、const オブジェクトだけでなく非 const オブジェクトも含め、任意のタイプのオブジェクトに対して呼び出すことができます。
  • オブジェクトを const として宣言する場合は、宣言時に初期化する必要があります。ただし、宣言中のオブジェクトの初期化は、コンストラクターの助けを借りてのみ可能です。
  • 関数の宣言で const キーワードが使用されると、関数は const になります。 const 関数の考え方は、関数が呼び出されるオブジェクトを変更できないようにすることです。
  • オブジェクトに対する誤った変更を避けるために、できるだけ多くの関数を const にすることをお勧めします。

Const メンバー関数の例

例1

以下の C++ プログラムは、定数ではないメンバー関数でデータ メンバーを更新できることを示しています。

C++






// C++ program to demonstrate that data members can be> // updated in a member function that is not constant.> #include> using> namespace> std;> class> Demo {> >int> x;> public>:> >void> set_data(>int> a) { x = a; }> >// non const member function> >// data can be updated> >int> get_data()> >{> >++x;> >return> x;> >}> };> main()> {> >Demo d;> >d.set_data(10);> >cout << d.get_data();> >return> 0;> }>

>

>

出力

11>

例 2

以下の C++ プログラムは、Constant メンバー関数ではデータを更新できないことを示しています。

C++




// C++ program to demonstrate that data cannot be updated> // in a Constant member function> #include> using> namespace> std;> class> Demo {> >int> x;> public>:> >void> set_data(>int> a) { x = a; }> >// constant member function> >int> get_data()>const> >{> >// Error while attempting to modify the data> >// member> >++x;> >return> x;> >}> };> main()> {> >Demo d;> >d.set_data(10);> >cout << endl << d.get_data();> >return> 0;> }>

>

>

出力

./Solution.cpp: In member function 'int Demo::get_data() const': ./Solution.cpp:17:11: error: increment of member 'Demo::x' in read-only object ++x; ^>

例 3

以下の C++ コードは、クラス定義の外で定数メンバー関数を定義する方法を示し、定数メンバー関数を使用してプライベート メンバー変数の値を設定および取得する方法を示しています。

C++


C++分割文字列



// Constant member function defined outside the class> #include> using> namespace> std;> class> Demo {> >int> x;> public>:> >void> set_data(>int>);> >// const member function> >int> get_data()>const>;> };> // Function definition for setting the value of x.> void> Demo::set_data(>int> a) { x = a; }> // Function definition for retrieving the value of x (const> // member function).> int> Demo::get_data()>const> {>return> x; }> main()> {> >Demo d;> >// Set the value of x to 10 using the non-const member> >// function.> >d.set_data(10);> >// Print the value of x using the const member function.> >cout << d.get_data();> >return> 0;> }>

>

>

Javaスレッドの作成
出力

10>

例 4

以下の C++ プログラムは、const 関数が非 const オブジェクトによって呼び出せることを示しています。

C++




// C++ program to demonstrate that const functions can be> // called by non const objects> #include> using> namespace> std;> class> Test {> >int> value;> public>:> >Test(>int> v = 0) { value = v; }> >// const member function> >int> getValue()>const> {>return> value; }> };> int> main()> {> >// non const object> >Test t(20);> >cout << t.getValue();> >return> 0;> }>

>

>

出力

20>

関数が const として宣言されている場合、その関数は任意のタイプのオブジェクトに対して呼び出すことができます。非 const 関数は、非 const オブジェクトによってのみ呼び出すことができます。

たとえば、次のプログラムにはコンパイラ エラーがあります。

C++




// C++ program that demonstrate that non-const functions can> // not be called by const objects> #include> using> namespace> std;> class> Test {> >int> value;> public>:> >Test(>int> v = 0) { value = v; }> >// non const member function> >int> getValue() {>return> value; }> };> int> main()> {> >// const object> >const> Test t;> >cout << t.getValue();> >return> 0;> }>

>

Javaのセット
>

出力

./d869c7ba-f199-4a67-9449-3936b5db4c5b.cpp: In function 'int main()': ./d869c7ba-f199-4a67-9449-3936b5db4c5b.cpp:14:24: error: passing 'const Test' as 'this' argument of 'int Test::getValue()' discards qualifiers [-fpermissive] cout << t.getValue();>

別の例を見てみましょう。

C++




// Demonstration of constant object,> // show that constant object can only> // call const member function> #include> using> namespace> std;> class> Demo {> >int> value;> public>:> >Demo(>int> v = 0) { value = v; }> >void> showMessage()> >{> >cout <<>'Hello World We are Tushar, '> >'Ramswarup, Nilesh and Subhash Inside'> >' showMessage() Function'> ><< endl;> >}> >// const member function> >void> display()>const> >{> >cout <<>'Hello world I'm Rancho '> >'Baba Inside display() Function'> ><< endl;> >}> };> int> main()> {> >// Constant object are initialised at the time of> >// declaration using constructor> >const> Demo d1;> >// d1.showMessage();Error occurred if uncomment.> >d1.display();> >return> (0);> }>

>

>

出力

Hello world I'm Rancho Baba Inside display() Function>

Const メンバー関数に関する FAQ

Q1.クラスの const オブジェクトは非 const メンバー関数を呼び出すことができますか?

答え:

いいえ、const として宣言されたオブジェクトは変更できません。したがって、オブジェクトを変更しないようにするために const メンバー関数のみを呼び出すことができます。

Q2.クラスの非 const オブジェクトは const メンバー関数を呼び出すことができますか?

答え:

関数が const として宣言されている場合、その関数は任意のタイプのオブジェクトに対して呼び出すことができます。