Paip. Improve user experience ---- custom & lt; shift operator overload

Source: Internet
Author: User

Paip. Improve user experience ---- custom <shift operator overload




Author Attilax, EMAIL: 1466519819@qq.com
Source: attilax Column
Address: http://blog.csdn.net/attilax


<Shift Operator Overloading... makes the code more natural... improves user experience ..
 
Header file
//////////////////
# Ifndef QDEBUGHIDEC9_H
# Define QDEBUGHIDEC9_H


# Include <ostream>
# Include <iostream>
# Include "windows. h"
// # Include <iostream. h>
// Extern ostream PrintAti;
Class qDebugHideC9
{
Public:
QDebugHideC9 ();
Public:


Const qDebugHideC9 & operator <(char * str) const; // reload the string type
Const qDebugHideC9 & operator <(HWND str) const; // reload the string type
Const qDebugHideC9 & operator <(WINBOOL str) const; // reload the string type


};






# Endif // QDEBUGHIDEC9_H


Cpp File
////////////////////////
# Ifndef QDEBUGHIDEC9_cppx
# Define QDEBUGHIDEC9_cppx
# Pragma once
# Include "qdebughidec9.h"


QDebugHideC9: qDebugHideC9 ()
{
// Return * this;
}


Const qDebugHideC9 & qDebugHideC9: operator <(char * str) const
{
// Printf ("% s", str );
Return * this; // Similarly, pay attention to it here ......
}
Const qDebugHideC9 & qDebugHideC9: operator <(HWND str) const
{
// Printf ("% s", str );
Return * this; // Similarly, pay attention to it here ......
}
Const qDebugHideC9 & qDebugHideC9: operator <(WINBOOL str) const
{
// Printf ("% s", str );
Return * this; // Similarly, pay attention to it here ......
}




QDebugHideC9 PrintAti;


# Endif


Call Manual
////////////////
# Include <qdebughidec9.cpp>
 
QDebugHidec922 <"--";
 
 
Refer:
///////////////////////
Cout_baidu Baike .htm
Well, we have basically seen the essence of cout. We can start to implement a simplified version of cout (Lite) by ourselves. to distinguish between them, we name the cout object named myout, the class to which the myout object belongs is MyOutstream. What we need to do is to reload a series of operator operators of different types for the MyOutstream class <operator function. For simplicity, Here we only implement the integer and string types (char *). In order to express the disconnection from iostream, we no longer use the header file iostream, but use the printf function in the old stdio for output. The program is very simple, including the complete main function, which is listed as follows:






That is, even if operators are overloaded, they are limited!
Friend ostream & operator <(ostream & OS, const CPolynomial &)
Operator is a keyword for operator overloading. It is followed by an overloaded operator, with a stream on the left and a stream processing object on the right. In this way, you can process how to input or output the object.




Upstairs is right ~~
This format is generally used for reload.
Ostream & operator <(ostream &, XX );


[Cpp] view plaincopy
Friend ostream & operator <(ostream & output, const PhoneNumber &);
Friend istream & operator> (istream & input, PhoneNumber &);
The operator overload function contains two parameters: one is the reference to istream/ostream, and the other is the reference to the User-Defined type. When the compiler encounters the statement cin> phone, the compiler will generate functions to call operator> (cin, phone). Similarly, when the compiler supervises cout <phone, the compiler will generate functions to call operator <(cout, phone, when the compiler supervises cout <phone, the compiler will generate a function to call operator <(cout, phone)


2) Note that a friend function is not a member function of the class, so the implementation of the friend function is the same as that of a common function. You do not need to ":" to indicate the class to which the member function belongs, only member functions use the ":" Scope symbol;


 

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.