运算符重载有哪双重所有格的两种形式式

2.4 运算符重载 (第二章 C++面向对象程序设计)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
2.4 运算符重载 (第二章 C++面向对象程序设计)
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
加入VIP
还剩7页未读,
定制HR最喜欢的简历
你可能喜欢一般运算符重载
在进行对象之间的运算时,程序会调用与运算符相对应的函数进行处理,所以运算符重载有两种方式:成员函数和友元函数。成员函数的形式比较简单,就是在类里面定义了一个与操作符相关的函数。友元函数因为没有this指针,所以形参会多一个。
// 运算符重载,这里又叫赋值函数
string& operator =(const string &other);
// 运算符重载,但这里要用友元函数才行
friend ostream& operator && (ostream &os,const string &str);
string &string::operator=(const string &other)
if(this == &other)
return *this;
delete []m_
m_data = NULL;
m_data = new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
return *this;
ostream& operator && (ostream &os,const string& str)
返回引用用于实现链式表达式
os&&str.m_
成员函数重载与友元函数重载的区别:
If you define your operator overloaded function as member function, then compiler translates expressions like&s1 + s2&into&s1.operator+(s2).&That means, the operator overloaded member function gets invoked on the first operand.&That is how member functions work!
But what if the first operand is not a class?&There's a major problem if we want to overload an operator where the first operand is not a class type, rather say&double.&So you cannot write like this&10.0 + s2. However, you can write operator overloaded member function for expressions like&s1 + 10.0.
To solve this&ordering&problem, we define operator overloaded function as&friend&IF it needs to access&private&members. Make it&friend&ONLY when it needs to access private members. Otherwise simply make it&non-friend non-member&function to&improve&encapsulation!
class Complex //复数类
private://私有
double//实数
double//虚数
Complex(double real=0,double imag=0)
this-&real=
this-&imag=
Complex operator+(int x);
Complex Complex::operator+(int x)
return Complex(real+x,imag);
int main()
Complex com1(5,10),
total=com1+5;
把上述main()主函数实现部分里的total=com1+5改为total=5+com1;那么程序就会报错(没有与这些操作数匹配的 "+" 运算符),因为左操作数5不是该复数类的对象,不能调用相应的成员函数Complex operator+(int x),所以编译错误。但如果我们定义一下两个友元函数就能解决上述的问题:
  friend Complex operator+(Complex com1,int x);
  friend Complex operator+(int x,Complex com1);
两种重载方式的比较:
一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。
以下一些双目运算符不能重载为类的友元函数:=、()、[]、-&。
类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。 C++提供4个类型转换函数:reinterpret_cast(在编译期间实现转换)、const_cast(在编译期间实现转换)、stactic_cast(在编译期间实现转换)、dynamic_cast(在运行期间实现转换,并可以返回转换成功与否的标志)。
若一个运算符的操作需要修改对象的状态,选择重载为成员函数较好。
若运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则只能选用友元函数。
当运算符函数是一个成员函数时,最左边的操作数(或者只有最左边的操作数)必须是运算符类的一个类对象(或者是对该类对象的引用)。如果左边的操作数必须是一个不同类的对象,或者是一个内部 类型的对象,该运算符函数必须作为一个友元函数来实现。(比如流操作符,即&&,&&)
当需要重载运算符具有可交换性时,选择重载为友元函数。
注意事项:
除了类属关系运算符"."、成员指针运算符".*"、作用域运算符"::"、sizeof运算符和三目运算符"?:"以外,C++中的所有运算符都可以重载。
重载运算符限制在C++语言中已有的运算符范围内的允许重载的运算符之中,不能创建新的运算符。
运算符重载实质上是函数重载,因此编译程序对运算符重载的选择,遵循函数重载的选择原则。
重载之后的运算符不能改变运算符的优先级和结合性,也不能改变运算符操作数的个数及语法结构。
运算符重载不能改变该运算符用于内部类型对象的含义。它只能和用户自定义类型的对象一起使用,或者用于用户自定义类型的对象和内部类型的对象混合使用时。
运算符重载是针对新类型数据的实际需要对原有运算符进行的适当的改造,重载的功能应当与原有功能相类似,避免没有目的地使用重载运算符。
阅读(...) 评论()C++中 运算符重载 到底有什么何意义?_百度知道
C++中 运算符重载 到底有什么何意义?
不就和用一个其他名字的函数定义一个运算方式一样的吗,请说得通俗点,用例说明最好,先谢谢?请高手指点一直想不透这个问题,运算符重载与没重载前有什么不同,用operator定义一个运算符重载函数,我层次交浅!!
我有更好的答案
  运算符重载是一种特殊的函数重载。  c++的一大特性就是重载(overload),通过重载可以把功能相似的几个函数合为一个,使得程序更加简洁、高效。在c++中不止函数可以重载,运算符也可以重载。  由于一般数据类型间的运算符没有重载的必要,所以运算符重载主要是面向对象之间的。  在进行对象之间的运算时,程序会调用与运算符相对应的函数进行处理,所以运算符重载有两种方式:成员函数和友元函数。  成员函数的形式比较简单,就是在类里面定义了一个与操作符相关的函数。友元函数因为没有this指针,所以形参会多一个。
运算符重载是一种特殊的函数重载。例如int a,b;则求a+b值时,在编译过程中使用的是函数调用:opertor+(a,b).主要作用就是使程序更加简洁,减少编程时间。
本回答被提问者采纳
如.//如果没有重载加号的话,这条就通不过编译了只是这个+号你同样可以定义一个add函数来操作,比如你重载了加号,其实是同样可以定义add()函数来完成相同的操作;/;初始化a、b的语句c = a+b; /&#47..:c = a的确是一样的。重载是为了让运算比较符合人的思维,比如定义一个矩阵类matrixmatrix a, b ,c;&#47
你想想如果两个时间相加,你用+号行吗?你要是把+号重载一下,写一个你自己的+号运算方法不就可以了。运算符重载就是自己定义符号
其他1条回答
为您推荐:
其他类似问题
运算符重载的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。博主最新文章
博主热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)         
您现在的位置:&&>&&>&&>&&>&&>&正文
二级C++多态性:运算符重载中的两种形式
来源:()  【】 
  一般来说,单目运算符最好被重载为成员函数,对双目运算符最好被重载为友员函数。   例如: c+5.67   其中 c 是 complex 类的对象,上述表达式表明复数加上一个浮点数,这是有意义的。其结果是将浮点数加到复数的实部,虚部不变。   重载为成员函数: → c.operator+(5.67) → c.operator+(complex(5.67)) 重载为友员: → operator+(c,5.67) → operator+(c,complex(5.67)) 可行。  再考虑: 5.67+c   重载为友元: operator+(complex(5.67),c)   重载为成员函数: 5.67.operator+(c),j 就不对了。   所以,对双目运算符重载为友员函数比重载为成员函数更方便写。但是有的双目运算符还是重载为成员函数,如赋值运算符。 例如:将赋值运算符重载为成员函数: #include &iostream.h& class A { public: A(){X=Y=0;} A(int I,int j){X=I;Y=j;} A(A&p) { X.p.X;Y=p.Y; } A&operator=(A &p); Int getX(){ return X;} Int getY() { return Y;} private: int X,Y; } A&A::operator=(A&p) { X=p.X; Y=p.Y; Cout&&”ssignment operator called.“n”; Return * } void main() { A a(7,8); A B=a; Cout&&b.getX()&&”,”&&b.getY()&& } 执行结果: Assignment operator called. 7,8 说明: b=a, 解释为: b.operator=(a), 调用: A&A::operator=(A &p) 例如:重载增1减1运算符。 Obj++ 或 obj― #include &iostream.h& class counter { public: counter(){v=0;} counter operator ++(); counter operator ++(int); void print(){cout&&v&&} private:
}; counter counter::operator ++() { v++; return * } counter counter::operator ++(int) {
} void main() {
for (int I=0;I&8;I++) c++; c.print(); for(I=0;I&8;I++) ++c; c.print(); } 执行结果:8    16 说明:重载运算函数 counter operator ++() 为前缀运算符; counter operator ++(int) 为后缀运算符。 4 、插入符(&& )和提取符 (&&) 的重载。 例如:重载插入符和提取符对日期进行输出和输入。 #include &iostream.h& class Date { public: Date(int y,int m,int d) { Year=y;Month=m;Day=d;} friend ostream &operator&&(ostream &stream,Date &date); friend istream &operator &&(istream &stream,Date &date); private: int Year,Month,D }; ostream &operator &&(ostream &stream &stream,Date &date) { srteam&&date.Year&&”/”&&date.Month&&”/”&&date.Day&&
} istream &operator &&(istream &stream ,Date &date) { stream&&date.Year&&date.Month&&date.D
} void main() { Date Cdate(); Cout&&”Current date:”&&Cdate&& Cout&&”Enter new date:”; Cin&&C Cout&&”New date:”&&Cdate&& } 执行: current date:
Enter new date: New date:
分析:定义重载插入符时,使用 ostream 类,因为 cout 是从该类中派生出来的。定义重载提取符时,使用 istream 类,因为 cin 是从该类中派生出来的。重载的运算符函数 说明为类的友员函数,目的是为访问类中的私有成员。 例如:对于复数这种数据类型 的插入符和提取符进行重载。 #include &iostream.h& class complex { public: complex(){real=imag=0.0;} complex(double a,double b) { real=a;imag=b;} friend complex operator +(complex &c1,complex &c2); friend ostream &operator &&(ostream &stre,complex &c); friend istream &operator&&(istream &stre,complex &c); private: double real, } ; complex operator+(complex &c1,complex &c2) { double r=c1.real+c2. double I=c1.imag+c2. return complex (r,I); } osrteam &operator&&(ostream&stre,complex &c) { stre&&”(“&&c.real&&”,”&&c.imag&&”)”;
} istream&operator&&(istream &stre,complex &c) { stre&&c.real&&c. &&&2&&&
文章责编:gaoxiaoliang& 看了本文的网友还看了
?&&( 15:51:47)?&&( 14:16:20)?&&( 14:14:40)?&&( 14:13:00)?&&( 14:10:55)?&&( 14:02:35)
? ?   ? ?   ? ?   ? ?   ? ?
? ?   ? ?   ?
?   ? ?    ? ?   ? ?   ? ?   ? ?
? ?   ? ?
实用工具 |
| 大全 | 大全
     |
版权声明:如果网所转载内容不慎侵犯了您的权益,请与我们联系,我们将会及时处理。如转载本内容,请注明出处。
Copyright & 2004-
 网 All Rights Reserved 
中国科学院研究生院权威支持(北京) 电 话:010- 传 真:010-

我要回帖

更多关于 聚焦战略有两种形式 的文章

 

随机推荐