同じクラスにある 2 つのオブジェクトを追加しようとしています。
クラスのプライベート セクションには、2 つのint
変数があります。
class One {
private:
int num1, num2;
public:
One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object
friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
};
One operator+(const One&, const One&);// - a non-friend helper operator that adds One objects without changing their values and returns a copy of the resulting One
問題があるかどうかはわかりませopeartor+
ん
One operator+(const One &a, const One &b){
One c,d,r;
c = a;
d = b;
r += b;
r += a;
return r;
}
上記のコードは間違っていると思いますが、b.num1のように使用しようとすると、コンパイルエラーが発生します
error: 'int One::num1' is private
error: within this context
上記の関数はメンバー関数セクションにないため、 b->num1 も使用できません。
error: base operand of '->' has non-pointer type 'const One'
このように呼び込みますmain
Result = LeftObject + RightObject;