运算符重载和括号 [英] Operator overloading and parenthesis

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

问题描述

您好,


我是这个论坛的新用户,而且是C / C ++的新用户。如果你能帮助我解决我现在遇到的一个问题,我将不胜感激。


我准备了一个处理矩阵的课程(是的,那里他们中的很多都在附近,但我发现学习所有这些工作人员是一个很好的练习。我已经用*和+重载运算符定义了一些方法。


例如:

Hi there,

I''m a new user of this forum and quite of a newbei in C/C++. I''d appreciate it if you could help me with one problem I''m having at the moment.

I''ve prepared a class to deal with matrices (yes, there are many of them around, but I found it a good exercise to learn all this staff). I''ve defined some methods with a * and + overloaded operators.

For example:

展开 | 选择 | Wrap | 行号

推荐答案

你很幸运,整个事情都没有爆炸,或者让守护进程从你的鼻子里飞出来

因为你回来了对本地变量(temp)的引用,这是一个经典的

nono。首先定义运算符* =更改* this并返回一个引用

。根据运算符* =定义运算符*并获取矩阵类型参数

并返回任何运算符* =返回。


亲切的问候,

Jos
You are lucky the entire thing didn''t explode or made daemons fly out of your nose
because you''re returning a reference to a local variable (temp) which is a classic
nono. First define the operator*= which changes *this and returns a reference
to it. Define the operator* in terms of the operator*= and take Matrix type parameters
and return whatever operator*= returns.

kind regards,

Jos



你很幸运,整个事情都没有爆炸或让守护进程飞出你的鼻子

,因为你正在返回对局部变量(temp)的引用,这是一个经典的

nono。首先定义运算符* =更改* this并返回一个引用

。根据运算符* =定义运算符*并获取矩阵类型参数

并返回任何运算符* =返回。


亲切的问候,

Jos
You are lucky the entire thing didn''t explode or made daemons fly out of your nose
because you''re returning a reference to a local variable (temp) which is a classic
nono. First define the operator*= which changes *this and returns a reference
to it. Define the operator* in terms of the operator*= and take Matrix type parameters
and return whatever operator*= returns.

kind regards,

Jos



您好JosAH,


感谢您的回复,但我能'完全明白你是什么意思。你能用另一种方式表达它,或用一些代码草绘它吗?


万一它不清楚(我不知道你是否建议这个),我不想修改原始矩阵。我希望函数能够返回一个新函数,而无需修改旧函数。


再次感谢,


Guille

Hi JosAH,

Thanks for your reply, but I can''t fully understand what do you mean. Could you express it in another way, or sketch it with some code?

Just in case it wasn''t clear (I don''t know if you suggest this), I DON''T want to modify the original matrix. I want the function to give back a new one, without modifying the old one.

Thanks once again,

Guille



您好JosAH,


感谢您的回复,但我不能完全理解你是做什么的意思。你能用另一种方式表达它,或用一些代码草绘它吗?


万一它不清楚(我不知道你是否建议这个),我不想修改原始矩阵。我希望函数能够返回一个新函数,而无需修改旧函数。


再次感谢,


Guille
Hi JosAH,

Thanks for your reply, but I can''t fully understand what do you mean. Could you express it in another way, or sketch it with some code?

Just in case it wasn''t clear (I don''t know if you suggest this), I DON''T want to modify the original matrix. I want the function to give back a new one, without modifying the old one.

Thanks once again,

Guille



我不是乔斯,但是你返回后返回的矩阵是无效的 - 因为你在本地函数中声明它 - 你不能返回它(或对它的引用)。

I''m not Jos, but the matrix you''re returning is invalid after you return it - because you declared it in the local function - you can''t return it (or a reference to it).


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

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