C + + Super-faq "Operator overloading"

Source: Internet
Author: User

    • Which operators cannot be overloaded
. ?:::. * sizeof for some historical reasons,?: cannot be overloaded. If Overload expr1? EXPR2:EXPR3, there is no guarantee that only one of the EXPR2 or EXPR3 is executed. sizeof is an inline operator, and some operators rely on its implementation, so overloading is not allowed. Domain Descriptor:: Either side is not an object or an expression, but a name that is recognized by the compiler. ::performs a (compile time) scope resolution rather than an expression evaluation. 
    • Can programmers define their own operators?
I can't. This is not a language technical problem, but the overloaded post-expression content users are unable to unify the opinion, which can lead to serious problems. eg:* * For exponential operation, for A**b**c (a**b) **c or a** (B**C), the experts ' opinions cannot be unified.  
    • Operator overloading principle
  1. In accordance with the daily use of the overloaded, easy to use users;
  2. The mathematical operator should conform to the operation principle;
  3. The mathematical operator must have practical meaning to be available;
  4. Mixed-type mathematical operators must also have practical significance to be available;
  5. If constructive operators is defined, it is better to return by value;
  6. If the definition of constructive operators, it is best not to modify the operand;
  7. If the definition of constructive operators, it is best to allow the type of operation to promote;
  8. In general, your operator should change it operand (s) if and only if the operands get changed when you apply the same ope Rator to intrinsic types.
  9. If you customize the x++,++x and x+= operators, you must conform them to the general rules;
  10. If you define Pointer-like objects,*p, P[i], * (p+i) must conform to the general rules of use;
  11. Subscript operators typically require paired occurrences (const and non-const versions);
  12. Returns True if X==y is defined and only these two objects are behaviorally equivalent;
  13. If the size comparison operator is defined, the indirect comparison and the direct comparison results should be consistent;
  14. Avoid operator overloads that mislead users.
 Constructive operators refers to operators that can construct new objects after calculation. x++ and ++xshould the same observable effect on x, and should differ only with what they return. ++xshould return xby reference; x++should either return a copy (by value) of the original state of xor should have a voidReturn-type. 
    • How the subscript operation (subscript operator) character of a matrix class is overloaded
Use operator () instead of operator[]. Because the latter receives only one parameter, the former can receive any number of arguments. double& operator() (unsigned row, unsigned col);        // Subscript operators often come in pairsdouble operator () (unsigned row, unsigned col) const; Subscript operators often come in pairs for a two-dimensional matrix using operator[] access, actually returns a reference to an array of objects via operator[], and then returns a two-dimensional matrix element object through operator[]. The Array-of-array solution obviously works, but it's less flexible thanThe operator()Approach. Specifically, there is easy performance tuning tricks The can is done with the operator()approach that is more difficult in the [][]approach, and therefore the [][]approach is more likely to leads to the bad performance, at least in some cases.  the operator()approach is never worse than, and sometimes better than, the [][]approach.   m(i,j)gives you-a clean, simple-to-check all the parameters and to hide (and therefore, if you want to, change) the int Ernal data structure. 
    • How to overload the prefix and suffix operators
through a hidden parameter. class Number {
public:
  Number& operator++ ();    // prefix ++
  Number  operator++ (int); // postfix ++
};
 Note: The prefix version returns the reference, and the suffix version returns the value (or void). Because the suffix version assignment content is the pre-modified value, the this content has changed.

C + + Super-faq "Operator overloading"

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.