社区
C语言
帖子详情
重载+号运算符!
大道如海
2003-05-21 07:29:06
我在做一个复数的实例时,用到+号运算符的重载.
我试着写了一个,可以运行,但不知道符不符合运算符重载的规则,或者说下面的写法有没有不妥的地方。如果有,如何改正。
Complex Complex::operator+(Complex right)
{
realPart += right.realPart ;
imaginaryPart += right.imaginaryPart ;
return *this;
}
...全文
66
7
打赏
收藏
重载+号运算符!
我在做一个复数的实例时,用到+号运算符的重载. 我试着写了一个,可以运行,但不知道符不符合运算符重载的规则,或者说下面的写法有没有不妥的地方。如果有,如何改正。 Complex Complex::operator+(Complex right) { realPart += right.realPart ; imaginaryPart += right.imaginaryPart ; return *this; }
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
meteor135
2003-05-21
打赏
举报
回复
Complex Complex::operator+(Complex const &right)
{
realPart += right.realPart ;
imaginaryPart += right.imaginaryPart ;
return this; //这里对吗?this可是指针哦!改成return *this;
}
forever1314
2003-05-21
打赏
举报
回复
no problem!
大道如海
2003-05-21
打赏
举报
回复
TO:xieha(右边的话是正确的,左边的话是错误的)
似乎return this这句话有问题。
煜知搬砖者
2003-05-21
打赏
举报
回复
sorry,上面写错了:呵呵,
Complex Complex::operator+(Complex const &right)
{
realPart += right.realPart ;
imaginaryPart += right.imaginaryPart ;
return this;
}
煜知搬砖者
2003-05-21
打赏
举报
回复
Complex& Complex::operator+(Complex const &right)
{
realPart += right.realPart ;
imaginaryPart += right.imaginaryPart ;
return this;
}
realdreamer
2003-05-21
打赏
举报
回复
msdn 有例子:
// operator_overloading.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class Complex
{
public:
Complex( double r, double i ) : re(r), im(i) {}
Complex operator+( Complex &other );
void Display( ) { cout << re << ", " << im << endl; }
private:
double re, im;
};
// Operator overloaded using a member function
Complex Complex::operator+( Complex &other )
{
return Complex( re + other.re, im + other.im );
}
void main()
{
Complex a = Complex( 1.2, 3.4 );
Complex b = Complex( 5.6, 7.8 );
Complex c = Complex( 0.0, 0.0 );
c = a + b;
c.Display();
}
大道如海
2003-05-21
打赏
举报
回复
realPart,imaginaryPart为代表实部和虚部的成员变量。
c++
重载
+-
运算符
6-3
重载
+-
运算符
(10分) 请根据程序的输出结果,
重载
类A的+和-
运算符
。 类和函数接口定义: class A { public: A(int x = 0, int y = 0) : x(x), y(y) {} void show() const; A operator+(A& a); //
重载
...
c++
重载
++
运算符
_C ++中的
运算符
重载
c++
重载
++
运算符
C ++中的
运算符
重载
(Operator Overloading in C++) Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user ...
C++
重载
+
号
运算符
#include <iostream> #include <string> using namespace std;...1.成员函数
重载
运算符
; 2.自定义成员函数实现两个对象相加; 3.全局函数
重载
运算符
; /**********************************************
C++
重载
+、++、+=
运算符
C++
重载
+、++、+=
运算符
C++:前置++和后置++
运算符
重载
//前置++
运算符
重载
person& operator ++() { this->m_a++; return *this; } 后置++(先参与运算,再自加): //后置++
运算符
重载
,注意需要加int来与前置++
重载
做区分,语法这样规定。 person ...
C语言
69,371
社区成员
243,080
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章