C ++ overload and performance analysis increment, decrement operator

01 ++ - format operator overloaded function

Increment operator and decrement operators are front and rear of the points, such as:

a++  // 后置自增运算符
++a  // 前置自增运算符

b--  // 后置自减运算符
--b  // 前置自减运算符

To distinguish overridden by the operator is the front or rear operator, C ++ provides:

  • Pre-operator as one yuan operator overlays which member function has the following format:
T & operator++(); // 前置自增运算符的重载函数,函数参数是空
T & operator--(); // 前置自减运算符的重载函数,函数参数是空
  • Rear operator as two yuan operator overloading, write a useless parameter, the number of overloaded member function as follows:
T  operator++(int); // 后置自增运算符的重载函数,多一个没用的参数
T  operator--(int); // 后置自减运算符的重载函数,多一个没用的参数

02 discussed front and rear operator returns the value

Front and rear operator overloading function is as follows:
a member function of operator overloading pre | post operator overloading member functions
--- | ---
T & operator ++ (); | T operator ++ (int);
T & operator-- (); | T operator - ( int);
notice the difference yet? So the question is:

  • Why Front operator returns a reference &?
  • Why Rear operator returns an ordinary object (a temporary object)?

Mainly because the original C ++ in order to maintain front and rear operator characteristics:

  • Characteristics of the front operator
int a = 0

// (++a) = 5; 可以拆解成:
// a = a + 1; 
// a = 5;
(++a) = 5; // 前置++

After a first increment +1 to the value of a is 1, then a = participate in operation 5, the value of a is finally 5.

This shows that (a ++) returns a post-increment variable, a process variable in a subsequent calculation, the value of a variable will be modified. Therefore, the return value of the pre-overloaded function operator must be a reference &.

  • Operator characteristic post

Then home operator, can not be left as a value, i.e. (a++) = 5;is not established, the function of post-Overload operator returns the object is the normal value.

03 ++ - written operator overloading function

int main()
{
    CDemo d(10);
    cout << d++ << ","; // 等价于 d.operator++(0);
    cout << d << ","; 
    
    cout << ++d << ","; // 等价于 d.operator++();
    cout << d << ",";
    
    cout << d-- << ","; // 等价于 d.operator--(0);
    cout << d << ",";
    
    cout << --d << ","; // 等价于 d.operator--();
    cout << d << endl;
    
    return 0;   
}

Output:

10,11,12,12
12,11,10,10

Suppose you want to achieve results above the main function of the output, and how to write it?

First we defined the CDemoclass, but also the increment, decrement good operator overloading function definition.

class CDemo
{
public:
    CDemo(int i = 0):m_num(i) {} // 构造函数
    
    CDemo & operator++();    // 前置自增运算符重载
    CDemo  operator++(int);  // 后置自增运算符重载
    
    CDemo & operator--();    // 前置自减运算符重载
    CDemo operator--(int);   // 后置自减运算符重载

private:
    int m_num; // 成员变量
};

Then continue to achieve pre- increment, decrement operator overloading functions:

// 前置++
CDemo & CDemo::operator++()
{
    ++m_num;
    return *this;
}

// 前置--
CDemo & CDemo::operator--()
{
    --m_num;
    return *this;
}

Post- increment, decrement operator overloading, is a bit different, e.g. post ++, is involved in the first operation, then the self-energizing, the return value is not increased from the front of the object, specifically implemented as follows:

// 后置++
CDemo CDemo::operator++(int)
{
    CDemo tmp(*this); // 记录修改前的对象
    m_num--;
    return tmp;       // 返回修改前的对象
}

// 后置--
CDemo  CDemo::operator--(int)
{ 
    CDemo tmp(*this); // 记录修改前的对象
    m_num++;
    return tmp;       // 返回修改前的对象
}

Performance Comparison 04 front and rear operators

From the above example, we see that the step of performing post-overloaded function operator:

  1. First to produce a temporary object to hold the object before the increment or decrement is not;
  2. Member variable is then incremented or decremented;
  3. Finally, return to the object before the amendment;

The step of pre-overloaded function operator:

  1. Member variable increment or decrement;
  2. Returns the object reference;

Visible, front operator overloads than post overloads operator performance is higher, the overhead is relatively small.

Of course, those of ordinary variable type, such as int, double, long, etc., are front and rear performance difference is not great. It is important when we use iterator objects and for incrementing or decrementing, preferably with front operator manner, so that overhead can be reduced .

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11986332.html