任何机构都可以在运算符重载中解释这行代码
Can any body please explain this line of code in operator overloading for the plus
下面的代码用于使用运算符重载添加两个复数,但我不明白加号运算符的参数和主体。谁能解释一下?
complex operator+(complex a2) // why this argument is used please explain
{
complex a; //what is the need of this
a.r=r+a2.r; //and how it is being done
a.i=i+a2.i; //this one too
return a;
}
我知道复杂是一个类,a 是它的对象,但我幻想为什么我们使用 a2。
问题中的运算符处理将两个complex
对象相加以形成新的临时值的情况:
a1 + a2
当编译器看到该表达式时,它会创建第三个(临时)值,即它们的总和。
它通过将+
视为表达式左侧的成员函数来实现此目的。所以上面的代码等效于这个:
a1.operator+(a2); // member function call
所以函数的主体是对象a1
的成员函数的主体:
complex operator+(complex a2) // member function of a1 receiving a2 as a parameter
{
complex a; // this is the new value that will be created from a1 + a2
a.r = r + a2.r; // r is a1.r because this object is a1
a.i = i + a2.i; // same with i
return a; // the (temporary) result is returned
}
仅当临时结果分配给命名变量时,才会使用临时结果,如下所示:
a = a1 + a2;
complex operator+(complex a2) // why this argument is used please explain
{
complex a; //what is the need of this
a.r=r+a2.r; //and how it is being done
a.i=i+a2.i; //this one too
return a;
}
你的问题是"我不明白加号运算符的论点和主体"和"我知道复杂是一个类,a 是它的对象,但我幻想为什么我们使用 a2。
考虑一下,如果在定义重载函数时不complex a2
对象作为参数传递complex operator+()
它将产生错误消息,因为它需要一个参数。
当您在 main 函数中创建complex
类的对象时complex a2
帮助您传递任何其他对象的值以进行求和运算。
这是一个演示。
#include<iostream>
using namespace std;
class N{
private:
int x,y;
public:
N(int a=0,int b=0)
{
x=a;
y=b;
}
void print(){
cout<<x<<" : "<<y<<endl;
}
N operator+(N a)
{
x=a.x;
}
};
int main(){
N b;
N a(2,3);
a.print();
b.print();
b.operator+(a);
b.print();
return 0;
}
希望你已经了解complex a2
论点。如果没有,请在下面发表评论。
运算符重载通常意味着您可以执行比该运算符通常执行的操作更多的事情。 在这里,您使用"+"运算符重载添加两个复数。所以你正在做这样的事情。
结果 = A1+ A2,其中 a1 和 a2 是两个复数 分别有实部和虚部。 将 +operator 视为函数名称,那么如何将参数传递给函数。a2 是您要与 a1 相加的另一个数字,因此 a2 作为参数进入,该参数也是复杂类型,并且该函数将复杂对象返回为"a",其中包括实部和虚部。 我希望我回答你所寻求的。
complex operator+(complex a2) // why this argument is used please explain
{
complex a; //what is the need of this
a.r=r+a2.r; //and how it is being done
a.i=i+a2.i; //this one too
return a;
}
答 1.complex operator+(complex a2)
complex
指定返回类型将是type class complex
。complex a2
指定运算符将用于class complex
的对象。
答 2.complex a;
这是对类复合体的对象和数据成员执行临时操作所必需的。把它想象成一个temp
变量,你引入一个变量,在一个函数中将 2 个数字相加,稍后返回它。
答 3.a.r=r+a2.r;
好的,现在你已经欺骗了类复合体的一个临时对象,你说存储a2
的r
和调用这个重载函数r
的对象的总和,并将其存储到临时对象的r
中。
答 4.a.i=i+a2.i;
同上。
然后,您将临时对象return
a
计算的总和为r
和i
。请注意,a
的type
是class complex
的,因此返回类型在第 1 行中指定为complex
。
如果您还看到main()
函数中使用的运算符,您将更好地理解它。
complex x,y,z;
x=y+z;
这里x
将获取临时对象a
的值。调用变量y
,a2
将收到z
。就像在这里呼唤+
一样x = y.overloaded_plus(z) // only for explanation
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 有人能为我解释一下C++代码吗
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 有人可以解释一下这段代码如何能够反转字符串
- 有人可以向我解释一下这段代码的作用吗?
- 有人可以详细解释这个回文代码是如何工作的吗?
- 如何在 C++11 中解释静态代码
- 任何人都可以解释一下我是否需要 & 在第一个代码片段中
- 我不完全确定此代码的行为.有人可以向我解释一下吗?
- 有人可以解释我这个代码吗?它给出与我们输入的数字相反
- 我在 2D 数组的动态内存分配中遇到了一些奇怪的代码C++? 请解释一下这是什么?
- 解释以下宏代码的 O/P
- 如何解释 IDA Pro 生成的这些代码?
- 交叉和计算,任何人都可以解释代码吗?
- 请解释代码块"for(auto &it : iostr )"
- 需要解释C++代码的行为
- 解释C++代码
- 二叉搜索树的最低公共祖先解释代码
- 任何人都解释C++代码行
- 需要知道为什么代码以这种方式行为- C中的解释代码