C + + operator detailed

Source: Internet
Author: User
Tags arithmetic arithmetic operators
Overload operator parsing (original)

Overloaded operators are a good young man, but to spit it out, we often write many repetitive code for overloaded operators. It's boring, but it's also necessary. The more you overload, the greater the elasticity of your class. But, you can't do whatever you please. Play the game always follow the appropriate rules, write overloaded operators are also the case.

Here are the rules of the game: A unary operator can be a member function without parameters or a non member function with one argument. The binary operator can be a member function with one parameter or a non member function with two arguments. Operator=, operator[], operator (), operator-> can only be defined as member functions. The operator-> return value must be a pointer or an object that can use->. Overloaded operator++ and operator--with an int parameter to denote the suffix, without parameters to denote the prefix. In addition to operator new AND operator Delete, there must be at least one non-builtin data type in the overloaded operator argument. Overloaded operators should try to simulate the behavior of the operator's intrinsic type.

After seeing the rules of the game, we dismembered each part of the implementation and attention points.

Overloading of ⒈ input and output operators

For >> and << overloads, note the following points:

The ①io operator must be a non member function, and if it is defined as a member function, then the operating habits of the IO operator will be the opposite of the normal habit. How strange. At this point, you might ask, how do I invoke the private members of the object? Don't worry, we don't have friends or friends. This problem is solved by defining the IO operator overloaded function as a friend function of the class.

② during the input period, we may encounter errors. At this point, the object is to be restored to its original state. That is, we revert to what we were before we entered it.

③ this point is to pick the "C + + primer", I feel very good. We can overload the operator, which means we have a big space for freedom. But let's not forget the nature of the IO operator, don't overdo it, and minimize formatting.

After noticing a few points, let's look at an implementation example of a complete IO operator:

#include <iostream>
#include <string>
using namespace Std;
Class MyClass {
Private
    String name;
    int id;
    int prefix;
    int value;
Public
    MyClass () {};
    MyClass (string n, int a, int p, int nm): Name (n), id (a), prefix (p), value (nm) {}    ////Initialize member object with initialization list
    Friend Ostream &operator<< (ostream &stream, MyClass o);        When an operator is defined as a non-member function, it is defined as a friend of the class being manipulated
    Friend IStream &operator>> (IStream &stream, MyClass &o);    
};
Ostream &operator<< (ostream &stream, MyClass o)
{
    Stream << o.name << "";
    Stream << "(" << o.id << ")";
    Stream << o.prefix << "-" << o.value << "\ n";
    
}
IStream &operator>> (IStream &stream, MyClass &o)
{
    cout << "Enter name:";
    Stream >> O.name;
    cout << "Enter ID:";
    Stream >> o.id;
    cout << "Enter prefix:";
    Stream >> O.prefix;
    cout << "Enter value:";
    Stream >> O.value;
    cout << Endl;
    return stream;
}
int main ()
{
    MyClass A;
    Operator>> (Cin, a);        Equivalent to Operator>> (CIN, a)
    cout << A;                Equivalent to operator<< (cout, a)
    return 0;
}

I think that a lot of things are done in silence. Look at the code, you know, this guy is used, this is the standard. All right, then. The arithmetic operator and the relationship operator are described.

personal supplement: IO operators should not use the return value, in the use of the last convenient, more understandable

void operator<< (Ostream &stream, MyClass o)

cout<<obj;

void Operator>> (IStream &stream, MyClass &o)

cin<<obj;

Overloading of ⒉ arithmetic operators and relational operators

In general, the arithmetic operator and the relational operator are defined as non member functions.

① arithmetic operator

Let's see how the code is implemented:

#include <iostream>
#include <string>
using namespace Std;
Class Point
{
Public
    Point () {};
    Point (int x_, int y_): x (x_), Y (y_) {};
    Point (const point &p) {
        This->x = p.x;
        This->y = P.Y;
    };
    ~point () {};
    Friend Point operator+ (Point &p1, point &p2);    Add two objects
    Friend Point operator+ (int value, point &p1);    Sum of objects and values
    Friend Ostream &operator<< (ostream &os, point &p1);
Private
    int x;
    int y;
};
Point operator+ (Point &p1, point &p2)
{
    Point temp;
    temp.x = p1.x + p2.x;
    TEMP.Y = P1.y + p2.y;
    return temp;
}
Point operator+ (int value, point &p1)
{
    Point temp;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.