C++操作符重载总结

哪些操作符可以重载?

-不可重载操作符:“::、“?:、“.、“.*、“sizeof、“#、“##、“typeid

-其他操作符都可重载

操作符重载原则

-只能使用已有的操作符,不能创建新的操作符

-操作符也是函数,重载遵循函数重载原则

-重载的操作符不能改变优先级与结合性,也不能改变操作数个数及语法结构

-重载的操作符不能改变其用于内部类型对象的含义,它只能和用户自定义类型的对象一起使用,或者用于用户自定义类型的对象和内部类型的对象混合使用

-重载的操作符在功能上应与原有功能一致,即保持一致的语义

操作符重载的类型:成员函数或友元函数

-重载为类的成员函数:少一个参数(隐含this,表示二元表达式的左参数或一元表达式的参数

-重载为类的友元函数:没有隐含this参数

成员函数与友元函数

-一般全局常用操作符(关系操作符、逻辑操作符、流操作符)重载为友元函数,涉及对象特殊运算的操作符重载为成员函数

-一般单目操作符重载为成员函数,双目操作符重载为友元函数

-部分双目操作符不能重载为友元函数:“=、“()、“[]、“->

-类型转换操作符只能重载为成员函数

 

操作符重载的函数原型列表(推荐

// 操作符重载的函数原型列表(推荐)
// 普通四则运算
friend  A  operator + ( const A & lhs, const A & rhs );
friend  A  operator - ( const A & lhs, const A & rhs );
friend  A  operator * ( const A & lhs, const A & rhs );
friend  A  operator / ( const A & lhs, const A & rhs );
friend  A  operator % ( const A & lhs, const A & rhs );
friend  A  operator * ( const A & lhs, const int & rhs );    //  标量运算,如果存在
friend  A  operator * ( const int & lhs, const A & rhs );    //  标量运算,如果存在

// 关系操作符
friend  bool  operator == ( const A & lhs, const A & rhs ); 
friend  bool  operator != ( const A & lhs, const A & rhs );
friend  bool  operator < ( const A & lhs, const A & rhs );
friend  bool  operator <= ( const A & lhs, const A & rhs );
friend  bool  operator > ( const A & lhs, const A & rhs );
friend  bool  operator >= ( const A & lhs, const A & rhs );

// 逻辑操作符
friend  bool  operator || ( const A & lhs, const A & rhs );
friend  bool  operator && ( const A & lhs, const A & rhs );
bool  A::operator ! ( );

// 正负操作符
A  A::operator + ();    //  取正
A  A::operator - ( );    //  取负

// 递增递减操作符
A &  A::operator ++ ();	//  前缀递增
A  A::operator ++ ( int );	//  后缀递增
A &  A::operator --();	//  前缀递减
A  A::operator -- ( int );	//  后缀递减

// 位操作符
friend  A  operator | ( const A & lhs, const A & rhs );	    //  位与
friend  A  operator & ( const A & lhs, const A & rhs );    //  位或
friend  A  operator ^ ( const A & lhs, const A & rhs );	    //  位异或
A  A::operator << ( int n );    //  左移
A  A::operator >> ( int n );    //  右移
A  A::operator ~ ();  // 位反

// 动态存储管理操作符:全局或成员函数均可
void *  operator new( std::size_t size )  throw( bad_alloc );
void *  operator new( std::size_t size, const std::nothrow_t & )  throw( );
void *  operator new( std::size_t size, void * base )  throw( ); 
void *  operator new[]( std::size_t size )  throw( bad_alloc );
void  operator delete( void * p );
void  operator delete []( void * p );

// 赋值操作符
A &  operator = ( A & rhs );
A &  operator = ( const A & rhs );
A &  operator = ( A && rhs );
A &  operator += ( const A & rhs );
A &  operator -= ( const A & rhs ); 
A &  operator *= ( const A & rhs );
A &  operator /= ( const A & rhs );
A &  operator %= ( const A & rhs );
A &  operator &= ( const A & rhs );
A &  operator |= ( const A & rhs );
A &  operator ^= ( const A & rhs );
A &  operator <<= ( int n );
A &  operator >>= ( int n );

// 下标操作符
T &  A::operator [] ( int i );
const T &  A::operator [] ( int i )  const;

// 函数调用操作符
T  A::operator () ( … );    //  参数可选

// 类型转换操作符
A::operator char * ()  const;
A::operator int ()  const;
A::operator double ()  const;

// 逗号操作符
T2  operator , ( T1 t1, T2 t2 );    //  不建议重载

// 指针与选员操作符
A *  A::operator & ( );    //  取址操作符
A &  A::operator * ( );    //  引领操作符
const A &  A::operator * ( )  const;    //  引领操作符
C *  A::operator -> ( );    //  选员操作符
const C *  A::operator -> ( )  const;    //  选员操作符
C &  A::operator ->* ( … );    //  选员操作符,指向类成员的指针

// 流操作符
friend  ostream &  operator << ( ostream & os, const A & a );
friend  istream &  operator >> ( istream & is, A & a );


ref: http://www.xuetangx.com/courses/course-v1:TsinghuaX+20740084X+sp/about

 

基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值