Windows Native (24) and native24 are indispensable
[Download source code]
Indispensable Windows Native (24)-C ++: Operator overload, custom type conversion
Author: webabcd
Introduction
Windows Native C ++ is indispensable
- Operator overload
- Custom type conversion
Example
CppOperator. h
#pragma once #include <string>using namespace std;namespace NativeDll{ class CppOperator { public: string Demo(); };}
CppOperator. cpp
/** Operator overload, custom type conversion */# include "pch. h "# include" CppOperator. h "# include" cppHelper. h "# include <iostream> using namespace NativeDll; void cppoperator_demo1 (); void Merge (); void cppoperator_demo4 (); void Merge (); void Merge (); void cppoperator_demo7 (); void cppoperator_demo8 (); string CppOperator: Demo () {// reload the "+" operator cppoperator_demo1 () through the member function Overload the "*" operator cppoperator_demo2 () through the youyuan function; // use the "=" operator cppoperator_demo3 () through the youyuan function (); // use the member function to overload the "front ++" operator cppoperator_demo4 (); // use the member function to overload the "Rear ++" operator cppoperator_demo5 (); // you can use the <cppoperator_demo6 (); // type conversion function of ostream to implicitly or explicitly convert cppoperator_demo7 (); // use the constructor to implicitly convert cppoperator_demo8 (); // if the first operand is not an object of this class, you can only use the friend Method for reload (in this case, the member function cannot be used for RELOAD) // you can use the member function to reload the single-object operator. Binary operators // you cannot customize new operators. You can only reload existing operators. The operators that cannot be reloaded include: ". ",". * "," sizeof () ",": ","?: "Return" Let's see the code and comments ";} class CppOperatorComplex {private: string Name; public: CppOperatorComplex (): Name (" ") {} CppOperatorComplex (string name ): name (name) {} string ToString () {return Name;} // use the member function to overload the "+" Operator (this class object is on the left and the specified CppOperatorComplex object on the right) cppOperatorComplex operator + (CppOperatorComplex & coc); // reload the "*" operator through the Friends function (the left side is the specified CppOperatorComplex object, and the right side is the specified CppOperatorComplex object) friend CppOperatorComplex operator) friend bool operator = (string & name, CppOperatorComplex & coc); // use the member function to overload the "front ++" operator CppOperatorComplex operator ++ (); // reload the "Post ++" operator through the member function (an int type parameter must be added here, which is the Convention of c ++ to distinguish "front ++ ") cppOperatorComplex operator ++ (int); // reload ostream <friend ostream & operator <(ostream &, CppOperatorComplex &) through the youyuan function; // type conversion function, cppOperatorComplex can be converted (implicitly or explicitly) to int operator int () ;}; CppOperatorComplex: operator + (CppOperatorComplex & coc) {CppOperatorComplex result; result. name = this-> Name + "+" + coc. name; return result;} CppOperatorComplex operator * (CppOperatorComplex & coc1, CppOperatorComplex & coc2) {CppOperatorComplex result; result. name = coc1.Name + "*" + coc2.Name; return result;} bool operator = (string & name, CppOperatorComplex & coc) {CppOperatorComplex result; return name = coc. name;} CppOperatorComplex: operator ++ () // front ++ {this-> Name = "++" + this-> Name; return * this; // returns the current self-added object} CppOperatorComplex: operator ++ (int) /// post ++ {CppOperatorComplex temp (* this ); this-> Name + = "++"; return temp; // The object before auto-increment is returned} ostream & operator <(ostream & output, CppOperatorComplex & coc) {output <"name:" <coc. name; return output;} CppOperatorComplex: operator int () // defines the function of the overloaded operator {if (this-> Name = "webabcd") return 100; return 10 ;} // use the member function to overload the "+" operator void Merge () {CppOperatorComplex coc1 ("webabcd"); CppOperatorComplex coc2 ("wanglei"); CppOperatorComplex coc = coc1 + coc2; string result = coc. toString (); // webabcd + wanglei} // reload the "*" operator void cppoperator_demo2 () {CppOperatorComplex coc1 ("webabcd") through the youyuan function "); cppOperatorComplex coc2 ("wanglei"); CppOperatorComplex coc = coc1 * coc2; string result = coc. toString (); // webabcd * wanglei} // use the youyuan function to overload the "=" operator void cppoperator_demo3 () {string name = "wanglei "; cppOperatorComplex coc2 ("wanglei"); bool result = (name = coc2); // true} // reload the void cppoperator_demo4 () operator through the member function () {CppOperatorComplex coc ("wanglei"); string s1 = (++ coc ). toString (); // ++ wanglei string s2 = coc. toString (); // ++ wanglei} // reload the "Post ++" operator void cppoperator_demo5 () {CppOperatorComplex coc ("wanglei") through the member function "); string s1 = (coc ++ ). toString (); // wanglei string s2 = coc. toString (); // wanglei ++} // use the youyuan function to reload <void cppoperator_demo6 () {CppOperatorComplex coc ("wanglei "); cout <coc <endl; // name: wanglei} // demonstration of the type conversion function, implicit conversion and explicit conversion void cppoperator_demo7 () {CppOperatorComplex coc1 ("webabcd"); CppOperatorComplex coc2 ("wanglei"); // Since the result is of the int type, therefore, coc1 and coc2 are implicitly converted to int (implemented through "operator int ()") int result = coc1-coc2; // 90 // explicit conversion (implemented through "operator int ()") result = int (coc1)-int (coc2); // 90} class CppOperatorA {private: string Name; int Age; public: CppOperatorA (string name): Name (name), Age (0) {} CppOperatorA (int age): Age (age ), name ("") {} string ToString () {return Name + int2string (Age) ;}}; class CppOperatorB {private: string Name; int Age; public: explicit CppOperatorB (string name): Name (name), Age (0) {} explicit CppOperatorB (int age): Age (age), Name ("") {} string ToString () {return Name + int2string (Age) ;}}; // demonstrate how to implicitly convert void cppoperator_demo8 () {CppOperatorA a1 = "webabcd" Through constructors "; // The Compiler calls CppOperatorA (string name); the constructor CppOperatorA a2 = 100; // The Compiler calls CppOperatorA (int age); the constructor string result1 = a1.ToString (); // webabcd0 string result2 = a2.ToString (); // 100 // CppOperatorB b1 = "webabcd"; // The constructor CppOperatorB (string name); is modified to explicit, therefore, you cannot call this constructor implicitly. // CppOperatorB b2 = 100; // This constructor cannot be called implicitly because the constructor CppOperatorB (int age) is modified to explicit}
OK
[Download source code]