C ++ Operator Overloading and Operator Overloading

Source: Internet
Author: User

C ++ Operator Overloading and Operator Overloading

The operation objects of pre-defined operators in C ++ can only be basic data types. However, similar operations are also required for many user-defined types (such as classes. In this case, you must redefine these operators in C ++ and assign new functions to existing operators so that they can be used for specific operations of specific types. The essence of Operator Overloading is function overloading, which provides the scalability of C ++.

Operator Overloading is implemented by creating operator functions, which define the operations to be performed by overloaded operators. The definition of an operator function is similar to that of other functions. The only difference is that the name of an operator function is composed of the operator keyword and the operator symbol to be overloaded. The general format of operator functions is as follows:
<Return type description> operator <operator symbol> (<parameter table>)
{
<Function body>
}

(1) Apart from class Relational operators ".", member pointer operators ". *", scope operators ":", sizeof operators, and three-object operators "? : ", All operators in C ++ can be overloaded.

(2) Heavy-duty operators are restricted to operators that allow heavy-duty within the range of existing operators in the C ++ language. New operators cannot be created.

(3) Operator Overloading is essentially a function overload. Therefore, the compiler selects Operator Overloading Based on the function overloading principle.

(4) user-defined class operators must be reloaded before they can be used, but with two exceptions, operators "=" and "&" do not have to be reloaded.

 

Which operators can be used as overload? Almost all operators can be used as overload. Including:
Arithmetic Operators: =,-, *,/, %, ++ ,--
Bitwise OPERATOR:-, | ,~, ^, <, >>;
Logical OPERATOR :! &, |;
Comparison operators:>, <, >=, <=, = ,! =
Value assignment operator: =, + =,-=, * =, % =, \=^ =, <=, >>=;
Other operators: [], ()->, ', new, delete, new [], delete [],-> *.

 

There are two types of operators: member function form and friend function form. Both types can be private members in the category.

Instance: defines an int package class, encapsulates the int, and supports the + operator and the = Operator.

 

1. Use member functions to overload Operators

class Integer {
public:
Integer();
Integer(int value);
Integer operator+(int value);
void operator=(int value);
private:
int m_value;
};

 

//integer.cpp
#include "integer.h"

Integer::Integer() {
m_value = 0;
}
Integer::Integer(int value) {
m_value = value;
}

Integer Integer::operator+(int value) {
int tmpValue = m_value + value;
return Integer(tmpValue);
}

void Integer::operator=(int value) {
m_value = value;
}



Example:

 

Integer integer = Integer (10 );
Integer tmpInteger = 100; // reload = Operator
TmpInteger = integer + 1; // overload + operator

 

2. Use a friend function to overload Operators

Using the member function overload operator, we implement integer + 1. But if it is 1 + integer, we must use the friend function overload + operator at this time:

 

// Integer. h
Class Integer {
Public:
Integer ();
Integer (int value );
Integer operator + (int value );
Void operator = (int value );
Operator int () const;
Private:
Int m_value;

Friend Integer operator + (int value, Integer integer );
};

// Integer operator + (Integer integer, int value); // This function cannot be declared; otherwise, it will conflict with the member function
Integer operator + (int value, Integer integer );

 

 

//integer.cpp
#include "integer.h"

Integer::Integer() {
m_value = 0;
}
Integer::Integer(int value) {
m_value = value;
}

Integer Integer::operator+(int value) {
int tmpValue = m_value + value;
return Integer(tmpValue);
}

void Integer::operator=(int value) {
m_value = value;
}
Integer::operator int() const {
return m_value;
}

Integer operator+(int value, Integer integer) {
int tmpValue = integer.m_value + value;
return Integer(tmpValue);
}

Example:

 

Integer integer = Integer (10 );
Integer tmpInteger = 100; // reload = Operator
TmpInteger = integer + 1; // reload Integer member functions + operators
TmpInteger = 1 + tmpInteger; // overload friend functions + operators

 

Conversion operator overload:

From the above code, we can see Integer integer = 1; (it is really convenient to call the constructor implicitly), but if you want to implement int I = Integer (100 ), to convert a custom type to an internal type or to an existing type, you must use the conversion operator.

The form of conversion operators is as follows:

X: operator T ()

T is the type.

The sample code is as follows:

// Integer. h
Class Integer {
Public:
Integer ();
Integer (int value );
Integer operator + (int value );
Void operator = (int value );
Operator int () const; // int conversion operator
Private:
Int m_value;

Friend Integer operator + (int value, Integer integer );
};

// Integer operator + (Integer integer, int value); // This function cannot be declared; otherwise, it will conflict with the member function
Integer operator + (int value, Integer integer );

 

//integer.cpp
#include "integer.h"

#include <stdio.h>
#include <stdlib.h>
Integer::Integer() {
m_value = 0;
}
Integer::Integer(int value) {
m_value = value;
}

Integer Integer::operator+(int value) {
int tmpValue = m_value + value;
return Integer(tmpValue);
}

void Integer::operator=(int value) {
m_value = value;
}
Integer::operator int() const {
return m_value;
}

Integer operator+(int value, Integer integer) {
int tmpValue = integer.m_value + value;
return Integer(tmpValue);
}

Example:

 

Integer integer = Integer (10 );
Integer tmpInteger = 100; // reload = Operator
TmpInteger = integer + 1; // reload Integer member functions + operators
TmpInteger = 1 + tmpInteger; // overload friend functions + operators
Int num = tmpInteger; // reload the int conversion operator.

 

Overload ++ OPERATOR:

Each prefix has two types of suffixes. to distinguish these two types of operations, the suffix operator is treated as a binary operator. The sample code is as follows:

 

// Integer. h
Class Integer {
Public:
Integer ();
Integer (int value );
Integer operator + (int value );
Void operator = (int value );
Operator int () const; // int conversion operator
Integer operator ++ (); // heavy load ++ Integer
Integer operator ++ (int value); // reload Integer ++
Private:
Int m_value;

Friend Integer operator + (int value, Integer integer );
};

// Integer operator + (Integer integer, int value); // This function cannot be declared; otherwise, it will conflict with the member function
Integer operator + (int value, Integer integer );

 

 

//integer.cpp
#include "integer.h"

Integer::Integer() {
m_value = 0;
}
Integer::Integer(int value) {
m_value = value;
}

Integer Integer::operator+(int value) {
int tmpValue = m_value + value;
return Integer(tmpValue);
}

void Integer::operator=(int value) {
m_value = value;
}
Integer::operator int() const {
return m_value;
}

Integer Integer::operator++() {
Integer tmpInteger;
tmpInteger = ++m_value;
return tmpInteger;
}
Integer Integer::operator++(int value) {
Integer tmpInteger;
tmpInteger = m_value++;
return tmpInteger;
}

Integer operator+(int value, Integer integer) {
int tmpValue = integer.m_value + value;
return Integer(tmpValue);
}

Example:

 

Integer integer = Integer (10 );
Integer tmpInteger = 100; // reload = Operator
TmpInteger = integer + 1; // reload Integer member functions + operators
TmpInteger = 1 + tmpInteger; // overload friend functions + operators
Int num = tmpInteger; // reload the int conversion operator.
TmpInteger = integer ++; // reload Integer ++
TmpInteger = ++ integer; // reload ++ Integer

 

Comparison of Two reload forms:
In most cases, it is possible to overload operators as member functions of the class and membership functions of the class. However, member function operators and member function operators have their own characteristics:
(1) In general, it is best to reload a single object operator as a member function of the class, and a binary operator as a friend function of the class.
(2) The following binary operators cannot be overloaded as class friend functions: =, (), [],->.
(3) A type conversion function can only be defined as a member function of a class, rather than a friend function of a class.
(4) If an operator needs to modify the object state, it is better to select reload as a member function.
(5) If the required operands (especially the first operand) of the operator want to have implicit type conversion, you can only use the youyuan function.
(6) When an operator function is a member function, the leftmost operand (or only the leftmost operand) it must be a Class Object of the operator class (or a reference to this class object ). If the operand on the left must be an object of different classes or an internal type, this operator function must be implemented as a friend function.
(7) When the overload operator is required to be interchangeable, select the overload as a friend function.

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.