A test of the C ++ operator overload and temporary non-heap Variables

Source: Internet
Author: User

Today, I carefully read the previous examples of Objective C ++ and more objective C ++ in the reference books, focusing on the practical methods of operator overloading, it looked great, and I did it myself. The compiler uses VC ++. Net 7.1 because the compiler supports the C ++ standard by 99%. Now we can post the test code and output for discussion by the comrades.
// CINT. h ///////////////////////////////////
# Include <iostream>
Using namespace STD;

Class CINT
{
Public:
CINT ();
CINT (int I );
CINT (CINT & CINT );
CINT (CINT * pcint );
CINT & operator = (const CINT & I );
CINT & operator = (int I );
Void operator <(char * Str );
Friend CINT operator + (const CINT & LHV, const CINT & rhv );
~ CINT ();
PRIVATE:
Int m_ I;
};

// CINT. cpp ///////////////////////////////////
# Include "CINT. H"

CINT: CINT (): m_ I (0)
{
Cout <"/n CINT: CINT () This =" <this;
}

CINT: CINT (int I): m_ I (I)
{
Cout <"/n CINT: CINT (int I) m_ I =" <m_ I <"This =" <this;
}

CINT: CINT (CINT & CINT): m_ I (CINT. m_ I)
{
Cout <"/n CINT: CINT (CINT & CINT) m_ I =" <m_ I <"CINT. m_ I = "<CINT. m_ I <"This =" <This <"& CINT =" <& CINT;
}

CINT: CINT (CINT * pcint): m_ I (pcint-> m_ I)
{
Cout <"/n CINT: CINT (CINT * pcint) m_ I = "<m_ I <" pcint-> m_ I = "<pcint-> m_ I <" This = "<This <" pcint = "<pcint;
}

CINT & CINT: Operator = (const CINT & I)
{
Cout <"/n CINT: Operator = (CINT & I) This =" <This <"& I =" <& I;
M_ I = I. m_ I;
Return (* This );
}

CINT & CINT: Operator = (int I)
{
Cout <"/n CINT: Operator = (int I) This = His =" <This <"I =" <I;
M_ I = I;
Return (* This );
}

Void CINT: Operator <(char * Str)
{
Cout <"/n CINT: Operator <(char * Str) This =" <This <"m_ I =" <m_ I <"" <STR;
}

CINT operator + (const CINT & LHV, const CINT & rhv)
{
Cout <"/n operator + (CINT & LHV, CINT & rhv) LHV =" <& LHV <"rhv =" <& rhv;
Return CINT (LHV. m_ I + rhv. m_ I );
}

CINT ::~ CINT ()
{
Cout <"/n CINT ::~ CINT () This = "<this;
}

// Main. cpp ///////////////////////////////////
# Include "CINT. H"

Void fun0 (cint I)
{
Cout <"/n fun0 (CINT I) & I =" <& I;
}

Void fun1 (CINT & I)
{
Cout <"/n fun1 (CINT & I) & I =" <& I;
}

Void fun2 (const CINT & I)
{
Cout <"/n fun2 (const CINT & I) & I =" <& I;
}

Void main ()
{
Cout <"cint I (2), temp (10 );";
Cint I (2), temp (10 );
Cout <"/ncint J (I );";
Cint j (I );
Cout <"/ncint K (& I );";
Cint k (& I );

Cout <"/nfun0 (I );";
Fun0 (I );
Cout <"/nfun1 (I );";
Fun1 (I );
Cout <"/nfun2 (2 );";
Fun2 (2 );

Cout <"/Ni = temp ;";
I = temp;
Cout <"/Ni = 20 ;";
I = 20;
Cout <"/Ni </" mck /";";
I <"mck ";
Cout <"/Ni = J + k ;";
I = J + K;
Cout <"/Ni = J + 6 ;";
I = J + 6;
Cout <"/Ni = 5 + J ;";
I = 5 + J;
Cin. Get ();
}

//// // The following is the output /////////////////////////
Cint I (2), temp (10 );
CINT: CINT (int I) m_ I = 2 This = 0012fec4
CINT: CINT (int I) m_ I = 10 This = 0012fed4
Cint j (I );
CINT: CINT (CINT & CINT) m_ I = 2 CINT. m_ I = 2 This = 0012fec8 & CINT = 0012fec4
Cint k (& I );
CINT: CINT (CINT * pcint) m_ I = 2 pcint-> m_ I = 2 This = 0012fed0 pcint = 0012fec4
Fun0 (I );
CINT: CINT (CINT & CINT) m_ I = 2 CINT. m_ I = 2 This = 0012 febc & CINT = 0012fec4
Fun0 (CINT I) & I = 0012 febc
CINT ::~ CINT () This = 0012 febc
Fun1 (I );
Fun1 (CINT & I) & I = 0012fec4
Fun2 (2 );
CINT: CINT (int I) m_ I = 2 This = 0012 fecc
Fun2 (const CINT & I) & I = 0012 fecc
CINT ::~ CINT () This = 0012 fecc
I = temp;
CINT: Operator = (CINT & I) This = 0012fec4 & I = 0012fed4
I = 20;
CINT: Operator = (int I) This = His = 0012fec4 I = 20
I <"mck ";
CINT: Operator <(char * Str) This = 0012fec4 m_ I = 20 mck
I = J + K;
Operator + (CINT & LHV, CINT & rhv) LHV = 0012fec8 rhv = 0012fed0
CINT: CINT (int I) m_ I = 4 This = 0012 fecc
CINT: Operator = (CINT & I) This = 0012fec4 & I = 0012 fecc
CINT ::~ CINT () This = 0012 fecc
I = J + 6;
CINT: CINT (int I) m_ I = 6 This = 0012 fecc
Operator + (CINT & LHV, CINT & rhv) LHV = 0012fec8 rhv = 0012 fecc
CINT: CINT (int I) m_ I = 8 This = 0012fed8
CINT: Operator = (CINT & I) This = 0012fec4 & I = 0012fed8
CINT ::~ CINT () This = 0012fed8
CINT ::~ CINT () This = 0012 fecc
I = 5 + J;
CINT: CINT (int I) m_ I = 5 This = 0012 fecc
Operator + (CINT & LHV, CINT & rhv) LHV = 0012 fecc rhv = 0012fec8
CINT: CINT (int I) m_ I = 7 This = 0012fed8
CINT: Operator = (CINT & I) This = 0012fec4 & I = 0012fed8
CINT ::~ CINT () This = 0012fed8

/////// Test Summary /////////////////////////
First, the parameter types of operators are set to the const common type, which is explicitly stated in article 19 of more than tive C ++, this is to enable the operator to accept implicit type conversion parameters. The test statement is called by fun2 (2) in main (), and the output is:
CINT: CINT (int I) m_ I = 2 This = 0012 fecc // implicit type conversion, constructed by INT 2 temporary object
Fun2 (const CINT & I) & I = 0012 fecc // the reference of the temporary object is passed to fun2
CINT ::~ CINT () This = 0012 fecc // fun2 ends, parses the temporary object
According to this, writing const In the parameter table is a way to improve the applicability of the function.
The second is the use of operator +, which is described in efficiency C ++ clause 19. If the operator is declared as a member of the class, the call to (CINT) I + (INT) 2 is correct, and (INT) 2 + (CINT) the I call is incorrect, which does not meet the exchange rate satisfied by the built-in int type. Therefore, operators are declared as friends. The test statement is the last two sentences in main (), I = J + 6; I = 5 + J; the output is:
I = J + 6;
CINT: CINT (int I) m_ I = 6 This = 0012 fecc // implicit type conversion, constructed by INT 6 temporary object
Operator + (CINT & LHV, CINT & rhv) LHV = 0012fec8 rhv = 0012 fecc // execute the operation specified by the operator
CINT: CINT (int I) m_ I = 8 This = 0012fed8 // construct another partially unknown object to return the result
CINT: Operator = (CINT & I) This = 0012fec4 & I = 0012fed8 // The returned result is copied to I, so the "=" operator is called.
CINT ::~ CINT () This = 0012fed8 // release a partially unknown object
CINT ::~ CINT () This = 0012 fecc // operator call ends, releasing a temporary object
I = 5 + J; // same as above

Related Article

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.