Chained operations in C + +

Source: Internet
Author: User

Code compilation Environment: Windows7 32bits+vs2012.

1. What is a chained operation

Chained operations are continuous operations (operations) using operators, which are characterized by the presence of two or more than two identical operators in a single statement, such as continuous assignment operations, continuous input operations, continuous output operations, continuous addition operations, and so on, as examples of chained operations.

Chain operation must be related to the problem of binding law, for example, the chain-operated assignment operation satisfies the right binding law, that is, a=b=c is interpreted as a= (B=C), and the chain output operation principle satisfies the left binding law, namely cout<

2. Chained operation of class type

In order to implement the chain operation of the class type, so that the chain operation can be carried out, the operator overload must meet certain requirements:
(1) Operation Fu Chong must not return a void type in a function.
Because the void type cannot participate in any operation, the operator overload function returns the void type, which is actually the possibility of blocking the chain operation.

(2) Overloading the assignment operator, if the object of the class is returned, then the chained assignment must be done with the help of the copy constructor. This does not understand that there will be a large operational overhead, but also to write the correct copy constructor. Examine the program below.

#include <iostream>using namespace STD;classcomplex{DoubleRealDoubleImage Public: Complex (DoubleR=0.0,DoubleI=0.0) {real=r;    Image=i; } Complex (Constcomplex& c) {cout<<"Copy Constructor"<<endl;        Real=c.real;    Image=c.image; }voidShow () {cout<<real<<"+"<<image<<"I"<<endl; } Complexoperator=(Constcomplex&);}; Complex Complex::operator=(Constcomplex& c) {real=c.real; Image=c.image;return* This;}intMainintargcChar* argv[]) {Complex C1 (2.3,4.5), C2,C3; C1.    Show ();    C3=C2=C1; C2.    Show (); C3.    Show (); GetChar ();}

The running result of the program is:

As you can see, the copy constructor is called two times during a continuous two-time assignment operation. For the first time, the return value (temporary object) of the function is constructed by C1, and a call to the copy constructor occurs at the time of the c2=c1 operation, and the return value of the assignment operation is still an object of the complex class when the second occurrence is assigned to C3. Another call to copy the constructor occurred. It is clearly not a good idea to let the assignment operation depend on the copy constructor.

Think: Complex& Complex::operator=(Complex& c){…} What will be the result?

Simply modify the declaration and definition of the overloaded function for the assignment operator as follows:

operator=(const Complex&);Complex Complex::operator=(const Complex& c){    real=c.real;    image=c.image;    return *this;}

The same is the above program, the output result is:

That is, one copy constructor is not called, because the copy operator function returns a reference to the complex class without creating a new temporary object, which greatly improves the efficiency of the program. Therefore, the overload of the assignment operator is written almost without exception in the form of a return reference.

3. Chain operation for input and output

Overloaded functions for the input operator (>>) and the output operator (>>) must return a reference, otherwise the chained operation cannot be completed.

In general, the input operator overloads are implemented using the following function prototypes:
istream& operator>> (istream&, classname&);

To implement the output operator overloading, the following function prototypes are used:
ostream& operator<< (ostream&, classname&);

If an operator function returns an object of the IStream or Ostream class, rather than a reference, a compilation error occurs. The reason for the error and the overloads for the input input operator, please look forward to my follow-up blog.

Reference documents

[1]] Chen Gang. Advanced Step-by-step tutorials for C + + [M]. Wuhan: Wuhan University Press, 2008.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Chained operations 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.