# 王老师 C++ 运算符重载 转换函数 第二讲

1.赋值函数的重载

#include "stdafx.h"
#include <malloc.h>
class stack
{
private:
int *sp, top, max;
void inflate();

public:
stack(int size = 10)
{
sp = (int *)malloc(sizeof(int) * size);
max = size;
top = 0;
}
int pop();
void push(int value);
stack & operator=(stack & rightValue);
};

//栈的容量增倍
void stack::inflate()
{
int index, *tp;
tp = (int *)malloc(sizeof(int) * max * 2);
for(index = 0; index < top; index++)
{
tp[index] = sp[index];
}
max += max;
free(sp);
sp = tp;
}

//出栈
int stack::pop()
{
if(top <= 0)
throw 1;
return sp[--top];
}

//入栈
void stack::push(int value)
{
if(top == max)
inflate();
sp[top++] = value;
}

//赋值函数
stack & stack::operator=(stack & rightValue)
{
top = rightValue.top;
max = rightValue.max;
sp = (int *)malloc(sizeof(int) * max);
for(int index = 0; index < max; index++)
{
sp[index] = rightValue.sp[index];
}
return *this;
}

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

2.[]的重载

#include "stdafx.h"
#include <malloc.h>
#include <iostream>
using namespace std;

class Array
{
private:
int *a, len;

void inflate()
{
cout << "the array is inflating..." << endl;

int *b = (int *)malloc(sizeof(int) * len * 2);
for(int i = 0; i < len; i++)
{
b[i] = a[i];
}
len += len;
free(a);
a = b;
}

public:
Array(int size):len(size)
{
a = (int *)malloc(sizeof(int) * size);
}

int & operator[](int index)
{
if(index < 0)
throw 1;
if(index >= len)
inflate();

return a[index];
}

void trans()
{
for(int i = 0; i < len; i++)
{
cout << a[i] << endl;
}
}

};

void main()
{
Array a(15);

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

a.trans();
}

3.()的重载

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;

//求最小值
template <typename T1, typename T2> T1 &min(T1 *x, int n, T2 cmp)
{
int j = 0;
for(int i = 1; i < n; i++)
{
if(cmp(x[i], x[j]))
{
j = i;
}
}
return x[j];
}

//函数对象
class Fun_obj
{
public:
bool operator()(int x, int y)
{
return x < y;
}
};

int cmp(int x, int y)
{
return x < y;
}

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 < num; i++)
min(a, len, fo);
finish = clock();
duration = (double)(finish - start); // / CLOCKS_PER_SEC;
cout << "min by function_object :" <<
duration << " seconds" << endl;

start = clock();
for(int i = 0; i < num; i++)
min(a, len, cmp);
finish = clock();
duration = (double)(finish - start); // / CLOCKS_PER_SEC;
cout << "min by function_pointer :" <<
duration << " seconds" << endl;
}

4.->的重载

（1）如果object是指针，所指对象需要有member成员；

（2）object是一个对象或对象引用，所属类必须重载->。计算->，得到obj（指针），做object = obj,转（1）。

#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
int x;

A(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 b(a);
cout << b->x << endl;
}

#include "stdafx.h"
#include <iostream>
using namespace std;
class Node
{
public:
int info;
Node * next;

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

class iter
{
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;
}
};

iter begin()
{
return iter(this);
}

iter end()
{
return iter();
}

void print()
{
iter p = begin();
iter 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 < this->info)
i++;
this->next = i.p;
if(i.q == NULL)
*h = this;
else
i.q->next = this;
}
};

void main()
{
}

int b = ++a;

a += 1;

int b = a;

int b = a++;

int temp = a;

a += 1;

int b = temp;

temp.~int();

int b = a;

a += 1;

C++标准规定：当为一个类重载“++”和“--”的前置版本时，不需要参数；当为一个类重载后置版本时，需要一个int类型的参数作为标志（即哑元，非具名参数）。至于问什么是这样而不是通过一个特殊的关键字来标识这种位置关系，参考《The Design and Evolution of C++》11.5.3节。

• 本文已收录于以下专栏：

举报原因： 您举报文章：王老师 C++ 运算符重载 转换函数 第二讲 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)