C ++ programming language has become an important application language in the development field. There are a lot of advanced content that deserves continuous exploration in practice. Here, we will first understand the application method of the C ++ operator overload to further interpret this programming language.
- What is the difference between the C ++ pointer and the reference?
- How to Implement C ++ file stream operations
- Sample Code for C ++ enum Enumeration
- Discussion on the Application of C ++ Dynamic Array
- Examples of various C ++ inheritance Methods
I. Why is the C ++ operator used for heavy loading?
For all operators of the system, generally, only the basic data types and classes provided in the standard library are supported. for user-defined classes, if you want to support basic operations, such as comparing the size, to determine whether the operators are equal, and so on, you need to define the specific implementation of this operator by yourself. For example, to determine whether two people are equally large, the default rule is to compare them by age. Therefore, when designing the class "person", we need to consider the operator =, and, according to the analysis just now, the comparison is based on age. So why is it heavy load? This is because the implementation version of the basic data type of this operator has been provided for us during the implementation of the compiler, but now its operand has become a user-defined data type class, so, you must provide the implementation of this parameter version by yourself.
2. How to declare a C ++ operator overload?
A: Operator Overloading is implemented as A class member function.
An overloaded operator is declared in the class body. The declaration method is the same as a common member function, except that its name contains the keyword operator and a c ++ predefined operator followed by it. You can declare a predefined = operator in the following way:
- class person{
- private:
- int age;
- public:
- person(int a){
- this->aage=a;
- }
- inline bool operator ==(const person &ps) const;
- };
The method for implementing the C ++ operator overload is as follows:
- inline bool person::operator==(const person &ps) const
- { if (this->age==ps.age)
- return true;
- return false;
- }
The call method is as follows:
- #include
- using namespace std;
- int main()
- { person p1(10);
- person p2(20);
- if(p1==p2) cout<<”the age is equal!”<return 0;
- }
Here, because operator = is a member function of class person, objects p1 and p2 can call this function. In the above if statement, it is equivalent to p1 calling function =, p2 is passed to the function as a parameter of the function to achieve comparison between two objects.
Consider the following if statement:
- if(10==p1) cout<<”the age is equal!”<
Is the execution performed correctly?
The answer is no, because it is considered as a class member overload operator only when the left operand is an object of the class type. Because 10 is not a person object, you cannot call the classperson operator =.
Consider the following if statement:
- if(person(10)==person(11))
- cout<<"ok"<
Can it be correctly executed? The answer is yes, because the operator is an anonymous object on both sides. The overloaded operators do not require the two operands to be of the same type. For example, we can define a subscript operator for class person to indicate the correspondence between the person and the phone number:
- /* Implement the subscript operator */
- # Include
- # Include
- Using namespace std;
- Class person
- {Private:
- Int tel;
- Public:
- Int & operator [] (string const & nm)
- {
- Return tel;
- }
- Int GetTel ()
- {
- Return tel;
- }
- };
- Int main ()
- {
- Person p1;
- P1 [& quot; suo & quot;] = 110;
- Person p2;
- P2 [& quot; rose & quot;] = 120;
- Cout <return 0;
- }
For operators that reload as member functions, the implicit this pointer is used as the first parameter of the function to represent the left operand.
B: the overload of the C ++ operator is implemented as a non-class member function (global function)
For global overload operators, parameters representing the left operand must be explicitly specified. For example:
- #include
- #include
- using namespace std;
- class person
- {
- public:
- int age;
- public:
- };
The private data of this class cannot be accessed outside the class. Therefore, you must set the age to public.
- Bool operator = (person const & p1, person const & p2)
- {
- If (p1.age = p2.age)
- Return true;
- Return false;
- }
- Int main ()
- {
- Person rose;
- Person jack;
- Rose. age = 18;
- Jack. age = 23;
- If (rose = jack)/* two objects represent the Left and Right operands respectively */
- Cout <"OK" <return 0;
- }
C: How can I decide whether to reload an operator as a class member function or a global namespace member?
① If an overload operator is a class member, the operator will be called only when the left operand used with it is the object of this class. If the left operand of the operator must be of another type, the operator must be reloaded as a member of the global namespace.
② C ++ requires a value =, subscript [], call (), and the member pointing-> operator must be defined as a class member operator. Any definition that defines these operators as namespace members will be marked as a compilation error.
③ If an operand is a class type such as a string class, it is best to define a symmetric operator such as an equal operator as a global namespace member.
D: The operator overload is the method of the friend function.
If you reload the C ++ operator as a friend function, you can directly access the private data member of the authorization class within the function, this is the main difference between the method of user functions and the global namespace method.
E: How can I determine whether a non-class member operator should be a friend of the class or use a member to access the function? In general, the implementer of the class should try to minimize the number of operators represented inside the namespace function and the operator class. If you have provided access to member functions and they have the same efficiency, it is best to use these member functions. However, if the implementer of A Class decides not to provide access member functions for some private members of the class and the namespace operator must reference these private members to complete their operations, they must use the membership mechanism. For example:
- #include
- #include
- using namespace std;
- class person{
- public:
- int age;
- public:
- };
- bool operator==(person const &p1 ,person const & p2)
- {
- if(p1.age==p2.age) return true;
- return false;
- }
- ostream operator<<(ostream &os,person const &p)
- {
- os<<"the person age is:"<return os;
- }
- int main()
- {
- person rose;
- person jack;
- rose.age=18;
- jack.age=23;
- cout</*call ostream operator<<(ostream &os,person const &p) */
- cout<return 0;
- }
Iii. Design of C ++ Operator Overloading
Class 1 designers cannot declare a non-predefined overload operator.
② Other operators cannot be defined for built-in data types.
③ The predefined operator priority cannot be changed.
④ Which operators should be provided for a class are determined by the intended use of the class.
4. prefix and postfix
To distinguish post-operator and pre-operator declarations, the declaration of the auto-increment and post-decimal operators for overload has an additional int type parameter. No parameter name is required because it is not used in operator definition. The extra integer parameter is transparent to users of the post operator, and the compiler provides it with the default value, so this parameter can also be ignored. For example:
- #include
- #include
- using namespace std;
- class person
- {
- private:
- int age;
- public:
- person(int a)
- {
- aage=a;
- }
- person const operator++()/*prefix ++ */
- {
- this->age++;
- return *this;
- }
- person const operator++(int a)/*postfix ++ */
- {
- person temp(1);
- temp=*this;
- this->age++;
- return temp;
- }
- int GetAge()
- {
- return age;
- }
- };
- int main()
- {
- person rose(10);
- person jack(20);
- person marry(22);
- person tom(30);
- jack=++rose;
- marry= tom++;
- return 0;
- }
The preceding section describes how to overload the C ++ operator.