C++-Overloaded operator to achieve array out-of-bounds detection & dividend detection

principle

C++Allows us to redefine the meaning of operators when they are used for class type objects. Through operator overloading, different operator versions can be defined for the operands of the class type.

Overload operator is a function of a special name: reserved word operatorfollowed for an operation of the symbol definition.

Like other functions, overloaded operators have return types and formal parameter lists, as follows:

Sales_item operator+(const Sales_item&, const Sales_item&);

The above code declares addition operator, the two may be used Sales_item对象by adding "and get a Sales_itemcopy of the object.

Except for the function call operator, the number of formal parameters (including member functions 隐式this 指针) of the overloaded operator is the same as the number of operands of the operator. The function call operator can accept any number of operands 1 .

So what are the operators that can be overloaded?

Insert picture description here
In particular, note that the following operators are not overloadable:

Insert picture description here
Also note that:

  • Not available for built-in types
    //以下方式是不被允许的
    int operator+(int, int);
    
  • Operator precedence and associativity will not change.
    That is, overloading operators will not change the precedence and associativity of operators.

Implement array out-of-bounds detection

Here we are overloaded [ ]operator. It is considered a binocular operator.

For example X[Y]can be seen:

  • [ ] Binocular operator
  • X Number of left operations
  • Y Right number of operations

Suppose Xthe object of a certain class, the class definition of overloading [ ]的 operator[ ] 函数, the expression:X[Y]

Can be interpreted as: X.operator[](Y) 2

Source code 3

array.h

class ARRAYSHARED_EXPORT Array
{
    
    
private:
    int* m_array;
    int SIZE;

public:
    Array(int len);
    int &operator[](int i);
};

array.cpp

Array::Array(int len)
{
    
    
    m_array = new  int[len];
    SIZE = len;

    for(int i = 0; i < len;i++){
    
    
        m_array[i] = i;
    }
}

int & Array::operator[](int i){
    
    
    if(i > SIZE){
    
    
        qDebug() << "Error: array[" << i <<"]数组越界";
        return m_array[0];
    }
    return m_array[i];
}

Implement dividend detection

Source code

hint.h

class Hint
{
    
    
    int int_number;

public:
    int get();
    void setint(int unm);
    Hint operator/(const Hint& b);
};

hint.cpp

int Hint::get(){
    
    
    return int_number;
}

void Hint::setint(int unm){
    
    
    int_number = unm;
}

Hint Hint::operator /(const Hint& b){
    
    
    Hint re;
    re.int_number = 0;

    if(0 == re.int_number){
    
    
        qDebug() << "Error: 被除数为0";
        return re;
    }
    re.int_number = this->int_number / b.int_number;
    return re;
}

Packaged as so library

You can refer to QT-Linux Dynamic Library & Static Library Creation and Usage Guide

use

    Array test_array(10);
    qDebug() << "array[2] = " << test_array[2];
    qDebug() << "array[12] = " << test_array[12];


    Hint m_hint1;
    Hint m_hint2;

    m_hint1.setint(20);
    m_hint2.setint(0);

    int re = (m_hint1 / m_hint2).get();
    qDebug() << m_hint1.get() << " / " << m_hint2.get() << " = " << re << endl;

effect

Insert picture description here
Using this method to implement detection is very rough. If you want to implement a detection tool, the detection tool implemented using this method will modify the user's source code, which is fatal to the user experience.

Reference thanks


  1. "C++Primer Fourth Edition" ↩︎

  2. C++ overloaded subscript operator [] ↩︎

  3. C ++ overload operators and overloads ↩︎

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/105787526