c – 使用if else进行对象比较运算符重载
发布时间:2020-12-16 09:35:23 所属栏目:百科 来源:网络整理
导读:我必须根据不同的标准比较(,,==)两个类对象,如下所述. class Student{ int iRollNumber; int iSection; int iMarks;} 我想与iRollNumber,iSection,iMarks(独立)进行比较. 我想与iRollNumber,iSection(Combined)进行比较. 我想与iMarks,iSection(Combined)进
我必须根据不同的标准比较(>,<,==)两个类对象,如下所述.
class Student { int iRollNumber; int iSection; int iMarks; } >我想与iRollNumber,iSection,iMarks(独立)进行比较. 目前我正在使用GetMethods()实现这一点,并使用if elseif elseif …结构来比较它们. 这导致到处乱码! 如果我使用运算符重载,我必须决定一种比较方式. 请建议一种优雅的编码方式. 要么 是否可以调用运算符重载多态? 解决方法
写命名函数:
int CompareR( const Student & a,const Student & b ); int CompareS( const Student & a,const Student & b ); int CompareM( const Student & a,const Student & b ); int CompareRS( const Student & a,const Student & b ); int CompareMS( const Student & a,const Student & b ); 虽然在课堂上进行这么多不同类型的比较的需要有点不寻常 – 你通常只需要一个或两个.函数应返回与strcmp()相同的值: < returns -1 == returns 0 > returns 1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |