2016-03-03 5 views
5

最近、C++で演算子のオーバーロードが検出されました。 あなたはクラスで演算子をオーバーロードしたい、と我々はそれで新しいオブジェクトを作成したい場合には、それは我々は、私はこの前の2つのオブジェクトを定義した場合、私は把握カントオペレータがC++でオーバーロードする

NameOfClass operator+(const NameOfClass& b){ 
    { 
     NameOfClass tmp; 
     tmp.length = this->length + b.length; 
     tmp.breadth = this->breadth + b.breadth; 
     tmp.height = this->height + b.height; 
     return tmp; 

} 

を行うことができます定義された他の人から作られています。例:

NameOfClass one(length,breadth,height); 
NameOfClass two(length,breadth,height); 

属性を設定します。 どうすればよいですか?

NameOfClass three=one+two; 

属性を「3」に設定していますか? "+"オーバーロードされた演算子の引数として1と2の両方が考慮されています。機能は明らかに

tmp.length = this->length + b.length; 

を言いますが、this->長さが未定義とのb.lengthがプライベートであるべきです。どのようにそれを一緒に混ぜるのですか? それはメソッドであるので、one+two = +は1と2のメソッドが引数として渡されています。つまり、this-> lengthは "one"オブジェクトの長さになりますか? tutorialspointの例を使用しています。

#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; 
     } 
     // Overload + operator to add two Box objects. 
     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;  // Length of a box 
     double breadth;  // Breadth of a box 
     double height;  // Height of a box 
}; 
// Main function for the program 
int main() 
{ 
    Box Box1;    // Declare Box1 of type Box 
    Box Box2;    // Declare Box2 of type Box 
    Box Box3;    // Declare Box3 of type Box 
    double volume = 0.0;  // Store the volume of a box here 

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

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

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

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

    // Add two object as follows: 
    Box3 = Box1 + Box2; 

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

    return 0; 
} 
+0

"+が1つと2つの方法が引数として渡されている"という疑いは正しいです。 – molbdnilo

答えて

5

1.

はどのように

NameOfClass three=one+two; 

は "3" の属性を設定していますか?

NameOfClass three=one+two;NameOfClass three = one.operator+(two);として解釈され、threeNameOfClass::operator=()の戻り値から構成されたコピー/移動するであろう。

2.

tmp.length = this->length + b.length; 

しかしthis->の長さは未定義であるべきとのb.lengthはプライベートです。どのように それを一緒に混ぜるのですか?

「this-> lengthは未定義」とはどういう意味ですか?ここではthis == &oneb == twoです。

b.lengthoperator+はメンバ関数であるため、privateは関係ありません、それプライベートメンバにアクセスできます。

2

NameOfClass three = one + two;匿名の一時的なone + twoからthreeを構築するためにコンパイラが生成しコピーコンストラクタを使用します。 one + twoは、実装したone.operator+(two)の構文砂糖です。それはが何かによって匿名一時的に使用することができないことは明らかだから

スマートコンパイラはよくコンパイラが生成する動きコンストラクタを使用することができます。

これらのコンストラクタを自分で実装することも、コンパイラに依存してメンバーデータをコピー(または移動)することもできます。

関連する問題