记录小demo,以备复习
运算符重载函数有三种定义方式:
1、定义为类的成员函数,这个多用于单目运算符,形参只有一个显示参数,另有一个this指针是隐形参 数。
2、定义为类的友元函数,多用于双目运算符,函数参数为两个显示参数,都需要写出来,同时运算符左边的对象对应函数的第一个形参,操作符右边的对应对应函的第二个形参。
3、定义为普通函数,普通函数于友元函数的区别是不能访问类里面的private 和 protected区域里的变量和函数,但是可以在普通函数里调用类里面public的函数,可以间接访问private里的变量。例子在下列代码中。
#include <iostream>
using namespace std;
class Box
{
public:
Box(){
length = 0;
breadth = 0;
height = 0;
}
friend Box operator+(const Box& a , const Box& b);
Box(double len , double bre , double hei):length(len) , breadth(bre) , height(hei)
{
cout << "the object has been made " << endl;
}
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
Box operator*( double n)
{
length = length * n ;
breadth = breadth * n;
height = height * n;
}
void show(){
cout << length << endl << breadth << endl << height << endl;
}
// 重载 + 运算符,用于把两个 Box 对象相加
/*Box operator+(const Box& b) //定义在了类的成员内部
{
Box box(0,0,0);
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}*/
friend Box operator*(int n , Box& box);
friend void operator <<(ostream &os , const Box& box);
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
void operator << (ostream &os , const Box &box)
{
os << "the values of box: " << box.height <<endl << box.breadth << endl << box.height <<endl;
}
Box operator+(const Box&a , const Box&b) //类外的非友元函数是不能直接访问private成员的!!!!!
{
Box box(0,0,0);
box.length = a.length + b.length;
box.breadth = a.breadth + b.breadth;
box.height = a.height + b.height;
return box;
}
Box operator*(int n , Box& box)//这个函数可以不写成友元函数,因为这个不涉及直接访问私有成员
{
return (box*n);
}
// 程序的主函数
int main( )
{
Box Box1(6.0,7.0,5.0); // 声明 Box1,类型为 Box
Box Box2(12.0,13.0,10.0); // 声明 Box2,类型为 Box
Box Box3(0,0,0); // 声明 Box3,类型为 Box
double volume = 0.0; // 把体积存储在该变量中
Box Box4;
/* Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);*/
// Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
Box4 = 2 * Box1;
Box4.show();
cout << Box4;
return 0;
}
以下为转载:http://c.biancheng.net/view/2314.html
内存管理运算符 new、new[]、delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数。一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内存时才会重载。
以成员函数的形式重载 new 运算符:
void * className::operator new( size_t size ){
//TODO:
}
以全局函数的形式重载 new 运算符:
void * operator new( size_t size ){
//TODO:
}
两种重载形式的返回值相同,都是void *类型,并且都有一个参数,为size_t类型。在重载 new 或 new[] 时,无论是作为成员函数还是作为全局函数,它的第一个参数必须是 size_t 类型。size_t 表示的是要分配空间的大小,对于 new[] 的重载函数而言,size_t 则表示所需要分配的所有空间的总和。
size_t 在头文件 <cstdio> 中被定义为typedef unsigned int size_t;,也就是无符号整型。
当然,重载函数也可以有其他参数,但都必须有默认值,并且第一个参数的类型必须是 size_t。
同样的,delete 运算符也有两种重载形式。以类的成员函数的形式进行重载:
void className::operator delete( void *ptr){
//TODO:
}
以全局函数的形式进行重载:
void operator delete( void *ptr){
//TODO:
}
两种重载形式的返回值都是 void 类型,并且都必须有一个 void 类型的指针作为参数,该指针指向需要释放的内存空间。
当我们以类的成员函数的形式重载了new 和 delete 操作符,其使用方法如下:
C * c = new C; //分配内存空间
//TODO:
delete c; //释放内存空间
如果类中没有定义 new 和 delete 的重载函数,那么会自动调用内建的 new 和 delete 运算符。