Application scenarios comparison of c ++ convert operator and other operator

Source: Internet
Author: User
When looking at the lftp source code, we found this line:

Operator bool () const {return ...}

I didn't think about it. What is this syntax? Is it operator overload? Unlike a, why not start with the return value of a function? Later I checked C ++ primer and understood that this is convert operator, which is to call this function if this class is to be converted to a bool value. Convert operator is prefixed with the operator keyword, followed by the type, followed by two parentheses (the brackets cannot contain parameters ). For more details, see C ++ primer and the above comments.

But there is a problem after reading:

Operator int () const {......}
Bool operator <(INT input ){......}

If both operators are defined (convert op and arithmetic OP), which one will be used in the actual code? Therefore, a short test code is written:

Code: select all
#include <iostream>
using namespace std;

class A {
    public:
        operator int() const { cout << "INT Convert function called..." << endl; return content; }
        bool operator<(int input) {
            cout << "Less operator function called..." << endl;
            if (content < input)
                return true;
            else
                return false;
        }
       
        void setContent(int value) { content = value; }
    private:
        int content;
};

int main()
{
    A a;
    a.setContent(10);
    if (a < 100)
        cout << "Class A is less than 100..." << endl;
    else
        cout << "Class A is larger than 100..." << endl;

    cout << "The abs of class A is: " << abs(a) << endl;
    return 0;
}

Test conclusion: if (a <100), arithmetic op is called; ABS (a) is called, and convert op is used. So now we have the answer:

1. When exact match exists, such as a <100, exact match is certainly used. If convert op is used to convert a to int, and then compare it, a convert process is required. <operator is a precise matching function. Therefore, you must use this function.

2. Arithmetic op cannot be used in the second call. Therefore, only convert op can be used to convert a to int. In addition to this situation, convert op is also used in some cases during actual encoding. For example, if <operator, not defined> is defined for a class, only convert op can be used when the class (if (a> 100) is used.

3. Avoid ambiguous. For example, if we write the code like this: if (a <100.00), then the compilation will fail. If arithmetic op is used, 100.00 must be converted to int to meet the arithmetic op parameter requirements. However, if convert op is used, the generated int must also be converted to double, can be compared with 100.00. In this way, the compiler will not be able to identify the priority of the two solutions, and the compilation will naturally fail.

OK, that's all.

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.