『C++』12 C++リロード演算子とリロード関数

6821 ワード

C++リロード演算子とリロード関数
C++では、同じ役割ドメイン内の関数と演算子に複数の定義を指定できます.それぞれ、関数リロードと演算子リロードと呼ばれます.リロード宣言とは、以前にこの役割ドメインで宣言された関数またはメソッドと同じ名前の宣言ですが、パラメータのリストと定義(実装)が異なります.リロード関数またはリロード演算子を呼び出すと、コンパイラは使用するパラメータタイプを定義のパラメータタイプと比較することで、最適な定義を選択することを決定します.最適なリロード関数またはリロード演算子を選択するプロセスを、リロード決定と呼びます.
C++の関数リロード
同じ役割ドメインでは、いくつかの機能が似ている同名関数を宣言できますが、これらの同名関数の形式パラメータ(パラメータの個数、タイプ、または順序を指す)は異なる必要があります.戻りタイプの違いだけで関数を再ロードすることはできません.次の例では、同じ名前の関数print()を使用して、異なるデータ型を出力します.
#include <iostream>
using namespace std;

class printData 
{
   public:
      void print(int i) {
        cout << "Printing int: " << i << endl;
      }

      void print(double  f) {
        cout << "Printing float: " << f << endl;
      }

      void print(char* c) {
        cout << "Printing character: " << c << endl;
      }
};

int main(void)
{
   printData pd;

   // Call print to print integer
   pd.print(5);
   // Call print to print float
   pd.print(500.263);
   // Call print to print character
   pd.print("Hello C++");

   return 0;
}

上記のコードがコンパイルおよび実行されると、次の結果が得られます.
Printing int: 5 Printing float: 500.263 Printing character: Hello C++
C++の演算子のリロード
ほとんどのC++に組み込まれている演算子を再定義または再ロードできます.これにより、カスタムタイプの演算子を使用できます.リロードされる演算子は特殊な名前の関数であり、関数名はキーワードoperatorとその後リロードされる演算子記号から構成されます.他の関数と同様に、リロード演算子には戻りタイプとパラメータのリストがあります.
Box operator+(const Box&);
加算演算子は、2つのBoxオブジェクトを加算し、最終的なBoxオブジェクトを返すために使用されます.ほとんどのリロード演算子は、通常の非メンバー関数として定義されてもよいし、クラスメンバー関数として定義されてもよい.上記の関数をクラスの非メンバー関数として定義する場合は、次のように、操作ごとに2つのパラメータを渡す必要があります.
Box operator+(const Box&, const Box&);
次の例では、メンバー関数を使用して、演算子の再ロードの概念を示します.ここで、オブジェクトはパラメータとして渡され、オブジェクトのプロパティはthis演算子を使用してアクセスされます.以下に示します.
#include <iostream>
using namespace std;

class Box
{
   public:

      double getVolume(void)
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
          length = len;
      }

      void setBreadth( double bre )
      {
          breadth = bre;
      }

      void setHeight( double hei )
      {
          height = hei;
      }
      //    +    ,      Box     
      Box operator+(const Box& b)
      {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
   private:
      double length;      //   
      double breadth;     //   
      double height;      //   
};
//       
int main( )
{
   Box Box1;                //    Box1,    Box
   Box Box2;                //    Box2,    Box
   Box Box3;                //    Box3,    Box
   double volume = 0.0;     //           

   // Box1   
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);

   // Box2   
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);

   // Box1    
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;

   // Box2    
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   //        ,   Box3
   Box3 = Box1 + Box2;

   // Box3    
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}

上記のコードがコンパイルおよび実行されると、次の結果が得られます.
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
リロード可能演算子/リロード不可演算子の下に、リロード可能演算子のリストがあります.
+
-
*
/
%
&
|
~
!
,
<
>
<=
>=
++
<<
>>
==
!=
&&
+=
-=
/=
%=
^=
=
*=
<<=
>>=
->
->*
new
new []
delete
次に、再ロードできない演算子のリストを示します.
::.* . ?: