Teacher Wang C ++ operator overload conversion function second lecture

xiaoxiao2021-04-11  1.8K+

1. Repeat function overload

The sample program code is as follows

#include "stdafx.h" #include Class Stack {private: int * SP, TOP, MAX; VOID INFLATE ();

Public: stack (int size = 10) Malloc (intend *); max = size; TOP = 0;} int push; void push; stack & operator = (stack & rightvalue);

// Stack capacity Double Void Stack :: Inflate () {INT INDEX, * TP; TP = (int *) malloc (sizeof (int) * max * 2); for (index = 0; index

// Find int Stack :: POP () {if (TOP <= 0) throw 1; Return SP [- TOP];

// Fort-Stack Void Stack :: Push (INT Value) {IF (TOP == max) inflate (); sp [top ] = value;

// Assignment function stack & stack :: operator = (stack & rightvalue) {top = rightvalue.top; max = rightvalue.max; sp = (int *) malloc (intend * max); for (int index = 0; Index

Void main () {stack x (100), y, z; z = y = x;}

It should be noted here that the return value of the assignment function is Stack &, which is to achieve a chain expression, such as z = y = x ;.

This is visible to "High Quality C / C Programming" (Lin Rui).

2. [] overload

Syntax: Value Type Operator [] (a metall parameter) function body

The sample program is as follows:

#include "stdafx.h" #include #include using namespace std;

Class Array {Private: Int * a, len;

Void inflate () {cout << "The array is inflaning ..." << endl;

INT * b = (int *) malloc (INT) * LEN * 2); for (int i = 0; i

Public: array (int size): len (size) {a = (int *) malloc (intend * size);}

INT & Operator [] (int index) {if (index <0) throw 1; if (index> = len) inflate (); return a [index];

Void trans () {for (int i = 0; i

}

void main () {Array A (15);

For (int i = 0; i <20; i ) {a [i] = i;}

A.TRANS ();

3. () overload

Sample program code:

#include "stdafx.h" #include #include #include using namespace std;

// Survey the minimum Template T1 & min (T1 * x, INT N, T2 CMP) {INT J = 0; for (INT i = 1; I

// Function object class fun_obj {public: BOOL OPERATOR () (int x, int y) {return x

INT CMP (INT X, INT Y) {RETURN X

Void main () {int a [] = {0, 1, 7, 9, 5, 4, -2, 3}; int LEN = SizeOf (a) / sizeof (int); fun_obj fo;

// cout << "min by function_Object:" << // min (a, sizeof (a) / sizeof (int), fo) << endl; // cout << "min by function_pointer: << // MIN (A, SizeOf (a) / sizeof (int), cmp) << ENDL;

Long Num = 100000000; Clock_T Start, Finish; Double Duration;

Start = clock (); for (int i = 0; I

START = clock (); for (int i = 0; i

4 .-> overload

Syntax: Value Type Operator -> () Function

The return value must be (1) pointer or (2) type of object or reference to the class

Calling grammar: Object-> member

Implementation process:

(1) If Object is a pointer, the object refers to some member members;

(2) Object is an object or object reference, and the class must be overloaded ->. Calculate ->, get OBJ (pointer), do Object = Obj, turn (1).

E.g:

#include "stdafx.h" #include using namespace std; class a {public: int x;

INT Value: x (value) {}

A * Operator -> () {Return this;}};

Class B {A & A; Public: B (A & Value): a (value) {}

A & Operator -> () {Return A;}};

Void main () {a a (99); b (a); cout << b-> x << endl;}

Example program (2):

#include "stdafx.h" #include using namespace std; class node {public: int info; node * next

Node (int value): info (value) {}

Class it {public: node * p, * q;

iTer (Node * u) {p = u; q = null;}

iter () {p = q = null;}

Iter & Operator () {if (p! = null) q = p; p = p-> next; return * this;}

Iter & Operator (int) {return Operator ();

BOOL Operator! = (iter i) {return p! = i.p;}

Node * Operator -> () {Return P;}};

{RETURN ITUR (this);}}}};

Void print () {{= begin (); it q = end (); while (p! = q) {cout << p-> info << Endl; p ;}}

Void INSERT (Node ** H) {Iter i = (* h) -> begin (), j = (* h) -> end (); while (i! = j && i-> info info ) I ; this-> next = IP; if (iq == null) * h = this; Else iq-> next = this;}};

Void main () {node * head = null; (New Node (2)) -> Insert (& head); (New Node (3)) -> Insert (& Head); (New Node (1)) -> Insert & head); (New Node (4)) -> INSERT (& Head); Head-> Print ();

It should be noted here that in the C program, the overload " " and "-" operators are important to understand their semantics.

INT B = A;

Semantics:

A = 1;

INT b = a;

and

INT b = a ;

Semantics is:

INT TEMP = a;

A = 1;

INT b = TEMP;

Temp. ~ Int ();

Instead of

INT b = a;

A = 1;

C standard specification: When a class overload " " and "-" are required, the parameters are not required; when a class is reloaded, you need an Int type parameter as a flag (ie Duten, non-name parameters). As to ask what is this, not through a special keyword to identify this positional relationship, refer to "The Design and Evolution Of C " 11.5.3.

When " " and "-" are applied to basic data types, the pre-versions and post versions are not much different in efficiency. When used for user-defined types, especially when large objects, the pre-version is much higher than that of the post version. The rear version always wants to create a temporary object and destroy it when exiting the function, and returns the value of the temporary object.

转载请注明原文地址:https://www.9cbs.com/read-133568.html

New Post(0)