C ++ Study Notes (9. Operator overloading)

Source: Internet
Author: User
Tags bitset

Knowledge point in this section: 1.c++ standard library:. the c ++ standard library is not part of the c ++ language. The c ++ standard library is a collection of class libraries and functions written in c ++.The classes and objects defined in the c ++ standard library are located in the std namespace, And the header files of the c ++ standard library are not included. h suffix, and the c ++ standard library covers the functions of the c library, such as in the c library Corresponds The B. c ++ standard library predefines most common data structures, such: How to Use cout and cin in c. c ++ standard library (the Code is as follows ):

#include 
 
  #include 
  
   using namespace std;int main(){int a;int b;printf("put a :\n");cin >> a;printf("put b :\n");cin >> b;cout << "sum is : " << a+b << endl;return 0;} 
  
 
2. operator Overloading of global functions: In a. c ++, function extension operators can be used through the operator keyword, that is, operator overloading, Operator implements operator Overloading through function overloading. Therefore, operator Overloading follows the function overloading rules.B. Definition of the class youyuan in c ++: The private statement prevents the class members from being accessed by the outside world, but the permission can be opened in the form of friends through the friend keyword.The sample code is as follows:
# Include
 
  
Using namespace std; class test {private: int a; int B; public: test (int a, int B) {this-> a =; this-> B = B;} int geta () {return a;} int getb () {return B ;} /* Note that youyuan can be any form of function */friend test operator + (test & a1, test & a2); // youyuan declaration}; test operator + (test & a1, test & a2) // The Global operator overload function {test c (0, 0); c. a = a1.a + a2.a; c. B = a1. B + a2. B; return c;} int main () {test n1 (); test n2 (); test n3 (); cout <"a is: "<n3.geta () <
  
   Note: The n1 + n2 in the Code is equivalent to operator + (n1, n2)3. Operator overloading of the member functions of the class: a. Using the member function overload OPERATOR: one parameter less than the global function, that is, the left operand, is also the object of this class. You do not need to use the friend keyword. The Code is as follows:
   
# Include
    
     
# Include
     
      
Using namespace std; class test {private: int a; int B; public: test (int a, int B) {this-> a =; this-> B = B;} test operator + (test & n2); friend test operator + (test & n1, test & n2 ); friend ostream & operator <(ostream & out, test & n) ;}; ostream & operator <(ostream & out, test & n) {out <"a is: "<n. a <
      
       
Note: The operator overload of the member function. The op1 symbol op2 is equivalent to the op1.operator symbol (op2). Therefore, op1 must be an operator overload of the global function of this class object, the op1 symbol op2 is equivalent to the operator symbol (op1, op2). So op1 and op2 can be of any type.
       
4. Application of operator overloading of member functions and global functions:. If the left operand is not a class object, you can only use the global function operator to overload it.B. When the class with the left operand cannot be modified, use the global function for reload., Just like the < <符号一样,因为不能修改ostream这个类,所以就只能使用全局函数的操作符重载 c. In fact, the operator overloading of a large number of member functions can be replaced by the operator overloading of global functions, but for the = value operator, [] array operator ,() function call operators and pointer operators can only be overloaded by member functions. (These four operators are special requirements in the c ++ compiler. They can only be overloaded using member functions. Do not ask why, because compilation may fail)D. The c ++ compiler provides a default value assignment operator for each class. The default value assignment operator only copies simple values. When there is a pointer member variable in the class (especially a pointer to the new memory), you need to overload the value assignment operator. Otherwise, when deleting the class in the destructor, it will delete the same address twice !!!The Code is as follows:
# Include
         
          
Using namespace std; class test {private: int a; int B; public: test (int a, int B) {this-> a =; this-> B = B;}/* test (const test & n) {cout <"hhhhhhh" <
          
           Note: first, the c ++ compiler provides a copy constructor for the class and a value assignment operator overload function. The two default functions share the same functions and are simple copies of the class member variables.
           Second, but the two functions are independent of each other and have their own application scenarios. For example, when test B = a; class definition is initialized, the copy constructor of the class is called. In addition, void fun (test a) is used when the function parameter is a class. When the function call is used to transmit parameters, the copy constructor is called to realize the transfer between real and virtual parameters. For example, when class B = a is directly assigned a value, the default value assignment operator overload function is called.
           Third, these two default functions are provided. Once defined by the user, the default functions provided by the c ++ compiler will become invalid.5. ++ Operator Overloading:.
           In c ++, a placeholder parameter is used to distinguish between pre-operation and post-operation.And the ++ operator can be a global function or a member function. The Code is as follows:
           
# Include
            
             
Using namespace std; class test {private: int a; int B; public: test (int a, int B) {this-> a =; this-> B = B;} friend ostream & operator <(ostream & out, test & n); friend test operator ++ (test & a, int ); friend test & operator ++ (test & a) ;}; ostream & operator <(ostream & out, test & n) {out <"a is: "<n. a <
             
              
Note: operator ++ (test & a, int) is a plus operation, and operator ++ (test & a) is a plus operation.
              
6. & | Operator overload:. Do not overload & and | operators, because the normal & and | built-in short-circuit rules are implemented, but Operator Overloading relies on function overloading, when the operands are passed as function parameters, all function parameters in c ++ are evaluated, and short-circuit rules cannot be implemented.The Code is as follows:
# Include
               
                
# Include
                
                 
Using namespace std; class Test {int I; public: Test (int I) {this-> I = I;} Test operator + (const Test & obj) {Test ret (0); cout <"Test operator + (const Test & obj)" <
                 
                  Note: Normally, the above t1 + t2 should be short-circuited and should not be executed. However, because t1 + t2 is essentially a function parameter, it is executed, the short-circuit rule is violated !!!
                  
7. summary in this section (several Frequently Asked Questions):. = value assignment operator, [] array operator, () function call operator, and-> pointer operator. These four operators can only be reloaded by member functions. ++ operator uses an int parameter to carry out front and back-loaded c. do not reload the & | Operator in c ++.

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.