【C++从入门到入土】第一篇:从C到C++.
【C++从入门到入土】第二篇:类和对象基础.
【C++从入门到入土】第三篇:类和对象提高.
complex_a + complex_b
所以让我们对运算符进行重载,满足自身的需求。
本文较长,请耐心阅读。
- 不可以引人新的运算符。除了 . 、.* 、:: 、? :四个运算符,其他的运算符皆可被重载。
- 运算符的操作数(operand)个数不可改变。每个二元运算符都需要两个操作数,每个一元运算符都需要恰好一个操作数。因此,我们无法定义出一个 equality运算符,并令它接受两个以上或两个以下的操作数。
- 运算符的优先级(precedence)不可改变。例如,除法的运算优先级永远高于加法。
- 运算符函数的参数列表中,必须至少有一个参数为 class类型。也就是说,我们无法为诸如指针之类的non-class类型,重新定义其原已存在的运算符,当然更无法为它引进新运算符。
定义形式:
返回值类型 operator 运算符(形参表)
{
……
}
重载实例:
class Complex
{
public:double real,imag;Complex( double r = 0.0, double i= 0.0 ):real(r),imag(i) { }Complex operator-(const Complex & c);
};
Complex operator+( const Complex & a, const Complex & b)
{return Complex( a.realal,a.imag+b.imag); // 返回一个临时对象
}
Complex Complex::operator-(const Complex & c)
{return Complex(real - c.real, imag - c.imag); // 返回一个临时对象
}
int main()
{Complex a(4,4),b(1,1),c;c = a + b; // 等价于c=operator+(a,b);cout << c.real << "," << c.imag << endl;cout << (a-b).real << "," << (a-b).imag << endl;//a-b 等价于a.operator-(b)return 0;
}
特点:
重载为成员函数时 , 参数个数为运算符目数减一 。
重载为普通函数时 , 参数个数为运算符目数 。
赋值运算符 ‘=’重载
有时候希望赋值运算符两边的类型可以不匹配,比如,把一个int类型变量赋值给一个Complex对象,或把一个 char * 类型的字符串赋值给一个字符串对象,此时就需要重载赋值运算符“=”。
代码如下 :
class String {
private:char * str;
public:String ():str(new char[1]){ str[0] = 0;}const char * c_str() { return str; };String & operator = (const char * s);~String( ) { delete [] str; }
};
String & String::operator = (const char * s)
{ // 重载“=”得 以使得 obj = “hello” 能够成立delete [] str;str = new char[strlen(s)+1];strcpy( str, s);return * this;
}
int main()
{String s;s = "Good Luck," ; //等价于 s.operator=("Good Luck,");cout << s.c_str() << endl;// String s2 = "hello!"; //这条语句要是不注释掉就会出错//因为这里不是使用赋值语句而是初始化,应调用构造函数s = "Shenzhou 8!"; //等价于 s.operator=("Shenzhou 8!");cout << s.c_str() << endl;return 0;
}
输出:
Good Luck,
Shenzhou 8!
引用上面的类说明此问题
有如下代码:
String S1, S2;
S1 = "this";
S2 = "that";
s1 = s2;
执行完这些语句,真的能达到我们想要效果吗?
当然没有,甚至很多时候会发生内存泄漏等重大错误。
因此要在 class String里添加成员函数,定义一个属于String类的赋值运算
String & operator = (const String & s)
{delete [] str;str = new char[strlen( s.str)+1];strcpy( str,s.str);return * this;
}
这么做就够了吗?还有什么需要改进的地方?
考虑下面语句:
String s;
s = "Hello";
s = s;
是否会有问题?
如果这样会开辟两片相同的空间,存放相同的数据,但是却都由s.str指向,明显错误,无法执行。
解决办法:
加一个判断两个变量是否相同
String & operator = (const String & s)
{if( this == & s)return * this;delete [] str;str = new char[strlen(s.str)+1];strcpy( str,s.str);return * this;
}
void 好不好?
String 好不好?
为什么是 String &
考虑: a = b = c;
和 (a=b)=c; //会修改a的值
分别等价于:
a.operator=(b.operator=( c ));
(a.operator=(b)).operator=( c );
上面的String类是否就没有问题了?
为 String类编写拷贝构造函数的时候,会面临和 = 同样的问题(浅深拷贝),用同样的方法处理。
String( String & s)
{str = new char[strlen(s.str)+1];strcpy(str,s.str);
}
例如:
class Complex
{double real,imag;
public:Complex( double r, double i):real(r),imag(i){ };Complex operator+( double r );
};
Complex Complex::operator+( double r )
{ //能解释 c+5return Complex(real + r,imag);
}
经过上述重载后:
Complex c ;
c = c + 5; // 有定义,相当于 c = c.operator +(5);
但是:
c = 5 + c; //编译出错
所以,为了使得上述的表达式能成立,需要将 + 重载为普通函数。
Complex operator+ (double r,const Complex & c)
{ //能解释 5+creturn Complex( c.real + r, c.imag);
}
但是普通函数又不能访问私有成员,所以,需要将运算符 + 重载为友元。
class Complex
{double real,imag;
public:Complex( double r, double i):real(r),imag(i){ };Complex operator+( double r );friend Complex operator + (double r,const Complex & c);
};
首先确定我们要实现的功能
代码如下:
int main()
{ //要编写可变长整型数组类,使之能如下使用:CArray a; //开始里的数组是空的for( int i = 0;i < 5;++i)a.push_back(i);CArray a2,a3;a2 = a; //重载“=”for( int i = 0; i < a.length(); ++i )cout << a2[i] << " " ;a2 = a3; //a2是空的for( int i = 0; i < a2.length(); ++i ) //a2.length()返回0cout << a2[i] << " ";cout << endl;a[3] = 100;CArray a4(a);//要自己写拷贝构造函数for( int i = 0; i < a4.length(); ++i )cout << a4[i] << " ";return 0;
}
接下来开始设置类
class CArray {
int size; //数组元素的个数
int *ptr; //指向动态分配的数组
public:CArray(int s = 0); //s代表数组元素的个数CArray(CArray & a);~CArray();void push_back(int v); //用于在数组尾部添加一个元素vCArray & operator=( const CArray & a);//用于数组对象间的赋值int length() { return size; } //返回数组元素个数_____ CArray::operator[](int i) //返回值是什么类型?{ //用以支持根据下标访问数组元素,// 如n = a[i] 和a[i] = 4; 这样的语句return ptr[i];}
};
上述重载下标运算符 [] 需要什么返回值呢?
因为我们需要支持如n = a[i] 和a[i] = 4; 这样的语句
所以需要引用,它能当左值。
具体函数实现:
CArray::CArray(int s):size(s)
{if( s == 0)ptr = NULL;elseptr = new int[s];
}
CArray::CArray(CArray & a)
{if( !a.ptr) {ptr = NULL;size = 0;return;}ptr = new int[a.size];memcpy( ptr, a.ptr, sizeof(int ) * a.size);size = a.size;
}
CArray::~CArray()
{if( ptr) delete [] ptr;
}
CArray & CArray::operator=( const CArray & a)
{ // 赋值号的作用是使“=” 左边对象里存放的数组,大小和内容都和右边的对象一样if( ptr == a.ptr) // 防止a=a 这样的赋值导致出错return * this;if( a.ptr == NULL) { // 如果a 里面的数组是空的//判断this->ptr是否为空if( ptr ) delete [] ptr;ptr = NULL;size = 0;return * this;}if( size < a.size) { // 如果原有空间够大,就不用分配新的空间if(ptr)delete [] ptr;ptr = new int[a.size];}memcpy( ptr,a.ptr,sizeof(int)*a.size);size = a.size;return * this;
}
void CArray::push_back(int v)
{ // 在数组尾部添加一个元素if( ptr) {int * tmpPtr = new int[size+1]; // 重新分配空间memcpy(tmpPtr,ptr,sizeof(int)*size); // 拷贝原数组内容delete [] ptr;ptr = tmpPtr;}else // 数组本来是空的ptr = new int[1];ptr[size++] = v; // 加入新的数组元素
}
因为语句cout<<5<<“this”;能成立,即<<能持续作用,既可作为右值也可作为左值,所以<<的返回值应该为ostream类的引用。
猜测代码如下:
ostream & ostream::operator<<(int n)
{…… // 输出n 的代码return * this;
}
ostream & ostream::operator<<(const char * s )
{…… // 输出s 的代码return * this;
}
cout << 5 << “this”;
本质上的函数调用的形式是什么?
cout.operator<<(5).operator<<(“this”);
当我们对一个类输出时,可对" << "重载:
ostream & operator<<( ostream & o,const String & s)
{o << s.ptr ;return o;
}
对" >> "也同样如此重载,需要访问类的私有成员时可重载为友元函数。
需要类和内置类型互相转换时可用
#include <iostream>
using namespace std;
class Complex
{double real,imag;
public:Complex(double r=0,double i=0):real(r),imag(i) { };operator double () { return real; }//重载强制类型转换运算符 double
};
int main()
{Complex c(1.2,3.4);cout << (double)c << endl; //输出 1.2double n = 2 + c; //等价于 double n=2+c.operator double()cout << n; //输出 3.2
}
自增运算符++、自减运算符–有前置/后置之分,为了区分所重载的是前
置运算符还是后置运算符,C++规定:
前置运算符作为一元运算符重载
重载为成员函数:
T & operator++();
T & operator–();
重载为全局函数:
T1 & operator++(T2);
T1 & operator—(T2);
后置运算符作为二元运算符重载,多写一个没用的参数:
重载为成员函数:
T operator++(int);
T operator–(int);
重载为全局函数:
T1 operator++(T2,int );
T1 operator—( T2,int);
接下来为函数实现:
class CDemo
{private :int n;public:CDemo(int i=0):n(i) { }CDemo & operator++(); // 用于前置形式CDemo operator++( int ); // 用于后置形式operator int ( ) { return n; }friend CDemo & operator--(CDemo & );friend CDemo operator--(CDemo & ,int);
}
CDemo & CDemo::operator++()
{ //前置 ++n ++;return * this;
} // ++s即为: s.operator++();CDemo CDemo::operator++( int k )
{ //后置 ++CDemo tmp(*this); // 记录修改前的对象n ++;return tmp; // 返回修改前的对象
}
CDemo & operator--(CDemo & d)
{ // 前置--d.n--;return d;
} //--s即为: operator--(s);
CDemo operator--(CDemo & d,int)
{ // 后置--CDemo tmp(d);d.n --;return tmp;
} //s--即为: operator--(s, 0);
类型转换函数重载
operator int ( ) { return n; }
这里int 作为一个类型强制转换运算符被重载, 此后
Demo s;
(int) s ; //等效于 s.int();
class MyPrint
{
public:void operator()(string text){cout << text << endl;}
};
void test01()
{//重载的()操作符 也称为仿函数MyPrint myFunc;myFunc("hello world");
}
class MyAdd
{
public:int operator()(int v1, int v2){return v1 + v2;}
};
void test02()
{MyAdd add;int ret = add(10, 10);cout << "ret = " << ret << endl;//匿名对象调用 cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main() {test01();test02();system("pause");return 0;
}
使用“类名()”时会产生一个该类匿名对象,起作用后便消亡。
后面会着重学习仿函数。
以上就是今天要讲的内容,介绍了基本的运算符重载,此部分内容需要动手实践才能发现一些隐藏的容易错误的点。
欢迎点赞评论,如有错误请指正。
本文发布于:2024-01-28 22:13:11,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170645119410651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |