重载“=”复数的运算符 [英] Overloading the "=" operator for Complex numbers

查看:91
本文介绍了重载“=”复数的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于为自己的学习目的创建一个复杂的课程

(通过做等等学习)。我又一次对某些事情感到困惑。我不能

弄清楚如何重载赋值运算符。


这就是我想要做的。我已经将课程复杂定义为


class Complex {

friend ostream& operator<<(ostream&,Complex&);

public:

复数(float = 0.0,float = 0.0);

复杂运营商+(复杂&);

复杂运算符 - (复杂&);

复杂运算符*(复杂&);

复杂运算符/(复杂&);

复杂运算符=(复杂&);

复共轭();

浮动幅度();

私有:

浮动重新;

浮动im;

};


构造函数是


复杂::复杂(浮动a,浮动b)

:re(a),im(b){}


我不知道如何编写重载函数。这不起作用


Complex Complex :: operator =(Complex& z)

{

re = z .re;

im = z.im;


返回* this;

}

救命!!!! :-)


提前致谢!


Pmb

解决方案

*" Pmb" <所以***** @ somewhere.com> schriebt:

我不知道如何编写重载函数。这不起作用

复杂复杂:: operator =(Complex& z)
{
re = z.re;
im = z.im ;

返回*这个;
}

帮助!!!! : - )




应该是=操作永远_change_右边的值

'=''的手边?


否?


在这种情况下,它应该是''const''。


你是否能够写作例如

a = b =复杂(1 ,2);





是吗?


在这种情况下,返回值应该是一个参考,以便它可以修改(例如,在结果上调用''='')。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么顶级发布这么糟糕的事情?

A :热门发布。

问:usenet和电子邮件中最烦人的事情是什么?




Alf P. Steinbach <人*** @ start.no>在消息中写道

news:40 **************** @ news.individual.net ...

*" PMB" <所以***** @ somewhere.com> schriebt:

我不知道如何编写重载函数。这不是
工作
复杂复杂:: operator =(Complex& z)
{
re = z.re;
im = z.im ;

返回*这个;
}

帮助!!!! :-)
应该是=操作永远_change_右边的'=''手边的价值?

没有?

在这种情况下,它应该是'' const''。




我对这个

点担心常量和程序完整性不感兴趣。永远不会使用此代码。这个特别的程序只是为了让我学习如何为操作员编写过载函数。


你是否应该能够编写例如

a = b =复杂(1,2);



是吗?

在这种情况下,返回值应该是参考这样它就可以被修改(例如,反过来在结果上调用''=''。




你是说像


Complex& operator =(Complex&);


Complex& Complex :: operator =(Complex& z)

{

< ??>

返还此商品;

}


我仍然没有看到在身体里放置什么来做这项工作。假设


class Complex {

friend ostream& operator<<(ostream&,Complex&);

public:

Complex(float = 0.0,float = 0.0);

复杂运算符+(Complex&);

复杂运算符 - (复数) &);

复杂运算符*(复杂&);

复杂运算符/(Complex&);

复杂运算符=(复杂&);

复共轭();

浮动幅度();

私人:

float re;

float im;

};


在我看来,运算符函数将是


Complex& Complex :: operator =(Complex& z)

{

re = z.re;

im = z.im;


将此退回;

}


但这不是不行。


Pmb


*" Pmb" <所以***** @ somewhere.com> schriebt:


Alf P. Steinbach <人*** @ start.no>在消息中写道
新闻:40 **************** @ news.individual.net ...

*" Pmb" <所以***** @ somewhere.com> schriebt:

我不知道如何编写重载函数。这不起作用
Complex Complex :: operator =(Complex& z)
{
re = z.re;
im = z.im;

返回*这个;
}

帮助!!!! : - )



应该是=操作永远_change_右边的'=''手边的价值?

没有?

在这种情况下,它应该是'' const''。



我对这个问题上的const和程序完整性不感兴趣。永远不会使用此代码。这个特别的程序只是为了让我学习为操作符编写重载函数。




如果拒绝考虑,你将不会学习C ++中的重载br />
''const''。


你是否应该能够写作

a = b =复杂(1,2);



是吗?

在这种情况下,返回值应该是参考,以便它可以被修改(例如,反过来在结果上调用''=''。



你的意思是

Complex& operator =(Complex&);

Complex& Complex :: operator =(Complex& z)
{
< ??>
返回这个;
}

我仍然没有看到在身体里放置什么来做这项工作。假设

类复杂{
朋友ostream& operator<<(ostream&,Complex&);
public:
Complex(float = 0.0, float = 0.0);
复杂运算符+(复杂&);
复杂运算符 - (复杂&);
复杂运算符*(复杂&);
复杂运算符/ (Complex&);
Complex& operator =(Complex&);
复共轭();
浮点数();
私有:
浮点数re;
float im;
};

在我看来,运算符函数将是

复杂&复杂::运算符= (Complex& z)
{
re = z.re;
im = z.im;

返回此内容;
}
但这不起作用。




相反,它不会_compile_。


First让它编译。<​​br />

-

答:因为它弄乱了人们通常阅读文本的顺序。

Q :为什么是顶级职​​位这么糟糕吗?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


I''ve been working on creating a Complex class for my own learning purpose
(learn through doing etc.). I''m once again puzzled about something. I can''t
figure out how to overload the assignment operator.

Here''s what I''m trying to do. I''ve defined class Complex as

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex operator=( Complex & );
Complex conjugate();
float magnitude();
private:
float re;
float im;
};

The constructor is

Complex::Complex( float a, float b )
: re( a ), im( b ) { }

I have no idea how to write the overload function though. This doesn''t work

Complex Complex::operator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}
Help!!!! :-)

Thanks in advance!

Pmb

解决方案

* "Pmb" <so*****@somewhere.com> schriebt:

I have no idea how to write the overload function though. This doesn''t work

Complex Complex::operator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}
Help!!!! :-)



Should the "=" operation ever _change_ the value that is on the right
hand side of ''=''?

No?

In that case, it should be ''const''.

Should you ever be able to write e.g.
a = b = Complex( 1, 2 );

?

Yes?

In that case, the return value should be a reference so that it can
be modified (e.g., in turn invoking ''='' on the result).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...

* "Pmb" <so*****@somewhere.com> schriebt:

I have no idea how to write the overload function though. This doesn''t work
Complex Complex::operator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}
Help!!!! :-)
Should the "=" operation ever _change_ the value that is on the right
hand side of ''=''?

No?

In that case, it should be ''const''.



I''m not interested in worrying about const and program integrity at this
point. This code will never be used. This particular program is simply for
me to learn about writing overload functions for operators.


Should you ever be able to write e.g.
a = b = Complex( 1, 2 );

?

Yes?

In that case, the return value should be a reference so that it can
be modified (e.g., in turn invoking ''='' on the result).



Do you mean something like

Complex &operator=( Complex & );

Complex &Complex::operator=( Complex &z )
{
<??>
return this;
}

I still don''t see what to place in the body to do the work. Suppose

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex &operator=( Complex & );
Complex conjugate();
float magnitude();
private:
float re;
float im;
};

It would seem to me that the operator function would be

Complex &Complex::operator=( Complex &z )
{
re = z.re;
im = z.im;

return this;
}

But this doesn''t work.

Pmb


* "Pmb" <so*****@somewhere.com> schriebt:


"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...

* "Pmb" <so*****@somewhere.com> schriebt:

I have no idea how to write the overload function though. This doesn''t work
Complex Complex::operator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}
Help!!!! :-)



Should the "=" operation ever _change_ the value that is on the right
hand side of ''=''?

No?

In that case, it should be ''const''.



I''m not interested in worrying about const and program integrity at this
point. This code will never be used. This particular program is simply for
me to learn about writing overload functions for operators.



You will not learn about overloading in C++ if you refuse to consider
''const''.


Should you ever be able to write e.g.
a = b = Complex( 1, 2 );

?

Yes?

In that case, the return value should be a reference so that it can
be modified (e.g., in turn invoking ''='' on the result).



Do you mean something like

Complex &operator=( Complex & );

Complex &Complex::operator=( Complex &z )
{
<??>
return this;
}

I still don''t see what to place in the body to do the work. Suppose

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex &operator=( Complex & );
Complex conjugate();
float magnitude();
private:
float re;
float im;
};

It would seem to me that the operator function would be

Complex &Complex::operator=( Complex &z )
{
re = z.re;
im = z.im;

return this;
}

But this doesn''t work.



Say rather, it does not _compile_.

First make it compile.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于重载“=”复数的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆