C ++: overloading math operators

I want to overload, say, the addition operator and add two objects of the same class. When I declare this prototype function β€œoperator +” in the class declaration in the header file, I pass both objects as parameters. I get a compiler error saying that the "binary" operator + "has too many parameters". I searched the answer on the Internet and found out that it declares an inline function only outside the class declaration in the assembled header file. I am wondering what I am doing wrong, or if I missed something. Here is the code I use in the header file.

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
    Person operator+(Person r, Person i);
};

This is a compilation with the error that I mentioned above. Below is the code that compiles fine.

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
};
inline Person operator+(Person r, Person i)
{
return Person(0,0); 
}
+3
8

oparator+ , this , , . , , const:

http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html

, :

class Person
{
    ...
    Person &operator+=(const Person &i);
    const Person operator+(const Person &i) const;
    ...
};

Person &Person::operator+=(const Person &i) {
    ...   // Do the compound assignment work.
    return *this;
}

const Person Person::operator+(const Person &i) const {    
    Person result = *this;     // Make a copy of myself.  Same as MyClass result(*this);
    result += i;            // Use += to add other to the copy.
    return result;              // All done!
}

const, , const this i. .

, , += , + += . , += .

, operator+ + =.

+6

- : this. . , operator+() , , , this.

, -, , , , (operator+() ), -, (operator+=() , ).
, . operator+() operator+=(), - -= ..
, , , const .

, operator+ :

class Person
{
public:
    Person& operator+=(const Person& i)
    {
      // whatever
      return *this;
    }
};

inline Person operator+(Person lhs, const Person& rhs)
{
  lhs += rhs; // note: lhs passed per copy
  return lhs;
}

. , , : . , , , , . . , .
, , +, . , ? ( , , , . :))
, Person , . ( , , .)

+6

+ = .

Vector3D& operator+=( const Vector3D& other ) {
        x+=other.x;
        y+=other.y;
        z+=other.z;
        return *this;
    }
+2

, , operator+ , -.

cplusplus.com:

- ?

" ++ IIRC".

geustgulkan:

, .

, ClassX, + . , ( objx), objx + 2. objx result ClassX.

, 2 + objx? , 2 + objx - BUT , , - 2 + objx .

, , ojbjects X +, int .

+ X.

class ClassX
{

    friend ClassX operator+(ClassX objx, int i);
    friend ClassX operator+(int i, ClassX objx);
};

, , :

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
    friend Person operator+(const Person &rhs, const Person &lhs);
};

Person operator+(const Person &lhs, const Person &rhs) {
     // do whatever constitutes addition here
     Person temp = lhs;
     temp.weight += rhs.weight;
     return temp;  // return copy of temp
}
+1

: Boost.Operators.

class Person: boost::addable<Person>      // yes private inheritance
{
public:
  Person& operator+=(Person const& rhs);

private:
};

, :

  • , , , ;)
  • addable

Boost.Operators , , , , .

.

+1

, - + - Person-: -, -, , .

- (, ), .

0
 class Person
  {
    private:
     int age;
     double weight;
    public:
      Person();                           //default constructor       
      ~Person();                           // destructor
      Person operator+(const Person &r); //overloaded operator as member function, so only one argument is required to be passed
 };

:

  Person Person::operator+(const Person &r)
  {
      Person x;
      x.age=this->age + r.age; // this refers to the object on which the function has been invoked, P1 in this case(see below)
      return x;
  }

(+ ) -, , , ,

,

 int main()
 {
    Person P1,P2;
    P2=P1+P2; //P1 is passed implicitly 

    //The above expression is equivalent to P2=P1.operator+(P2);
}
0

agsamek sbi.

  • - - 2 ( )
  • ofen, , "operator =" ;
  • 2 , . , ( ). "" " ;
  • ( .h ), manutenable. , discrection.

:

class Person
{
private:
    int age;
    double weight;
public:
    Person(){}                           //default constructor       
    Person(int a, double w) : age(a), weight(w) {}
    ~Person(){}                           //default desctructor

    int getAge() const { return age; }
    double getWeight() const { return weight; }

    const Person & operator =(const Person & rha) {
        age = rha.age; weight = rha.weight; return *this;
    }
};

Person operator +(const Person & lha, const Person & rha)
{
    return Person(lha.getAge() + rha.getAge(), rha.getWeight() + lha.getAge());
}

int _tmain(int argc, _TCHAR* argv[])
{
    Person p1(30,75);
    Person p2(40,80);

    Person sumOfPis = p1 + p2;  // <-- Without "operator =" this statement will use the default copy constructor

    return 0;
}
0

Source: https://habr.com/ru/post/1750884/


All Articles