通过类函数返回一个引用并在c ++中返回一个整个对象? [英] Return a reference by a class function and return a whole object in c++?

查看:247
本文介绍了通过类函数返回一个引用并在c ++中返回一个整个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运算符在类CVector中重载:

Operator overloading in class CVector:

CVector CVector::operator+ (CVector param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return (temp);
}

和主要:

CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;

因此,一个对象通过值传递,然后另一个临时对象正在创建。我猜b是通过值,a是调用+,因此x和y属于a和pram.x和param.y到b。返回temp并且复制赋值运算符将temp的值传递给c?

So an object is passed by value, and then another temp object is being created. I guess b is being passed by value, a is the one calling the + therefore the x and y belong to a and pram.x and param.y to b. temp is returned and the the copy assignment operator delivers temp's values to c?

但是这是什么:

CVector& CVector::operator= (const CVector& param)
{
  x=param.x;
  y=param.y;
  return *this;
}

和主要:

a=b;

再次a调用=和b通过引用传递给const。它是重要的,如果它是通过值?)这是我困惑,x属于a是分配param.x的be。所以为什么不是这个函数void,因为x和y可以通过这个函数访问。什么返回*这意味着,我知道这是调用函数的对象的地址,所以*这将是函数本身,但如果我们返回一个对象,我们需要将它分配到某个地方像前面的c = temp after temp = a + b?
什么是CVector&甚至意味着,它看起来不像我们期望一个CVector类型的对象的地址?

Again a is calling the = and b is being passed by reference as const.(in this case does it matter if it was passed by value?) This is where i get confused, x belonging to a is assigned param.x of be. so why isn't this function void, since x and y can be accessed by this function. What does return *this means, i know that this is the address of the object calling the function, so *this would be the function itself, but if we are returning an object we need to assign it it somewhere like the previous c=temp after temp=a+b? And what does CVector& even mean, it doesn't look like we are expecting an address of an object of CVector type?

换句话说,为什么不是该函数只是:

In other words why isn't the function just:

void CVector::operator= (const CVector& param)
    {
      x=param.x;
      y=param.y;
    }

然后有这个代码

#include <iostream> 
using namespace std;
 class Calc { 
private: 
  int value; 
public: 
  Calc(int value = 0) { this->value = value; } 
  Calc& Add(int x) { value += x; return *this; } 
  Calc& Sub(int x) { value -= x; return *this; } 
  Calc& Mult(int x) { value *= x; return *this; } 
  int GetValue() { return value; } }; 

int main() { 
  Calc cCalc(2); 
  cCalc.Add(5).Sub(3).Mult(4); 
  cout << cCalc.GetValue(); 
  return 0; 
}

现在如果我删除&从函数:

Now if i removed the & from the functions:

  Calc Add(int x) { value += x; return *this; } 
  Calc Sub(int x) { value -= x; return *this; } 
  Calc Mult(int x) { value *= x; return *this; } 

并使用

Calc cCalc(2)
cCalc.Add(5);
cCalc.Sub(3);
cCalc.Mult(4);

而不是前者,它会产生相同的resault。
那么为什么Calc&返回类型允许链接。

instead of the former, it would produce the same resault. So why does Calc& returne type allow chaining.

我不仅想知道如何编程,因为面向对象是很多写一个模式(这是这样写的,这是需要的if反对结构化编程,你必须使用逻辑,但也知道为什么是代码定义的和平,为什么它不是我直觉上认为它应该是(虽然我只学习编程的一个年)。

I not only want to know how to program, since object oriented is much writing by a pattern (this is written like this, this is needed if that) opposed to structured programming where you have to use logic, but also to know why is a peace of code defined as it is, and why it isn't as i intuitively think it should be(although i only learn programming for about a year).

谢谢!

推荐答案


所以为什么不是这个函数void,因为x和y可以被这个函数访问。什么是return *这意味着

so why isn't this function void, since x and y can be accessed by this function. What does return *this means

由于 operator =()返回一个引用, return * this; 返回对当前对象的引用。这允许我们链式分配运算符。例如,

Since operator=() is declared to return a reference, return *this; returns a reference to the current object. This allows us to chain assignment operators. For example,

a = b = c;

会调用 b.operator =(c); 并返回对 b 的引用。然后通过等价于 a.operator =(b)的调用分配

will call b.operator=(c); and return a reference to b. Then a is assigned by a call which is equivalent to a.operator=(b).

这篇关于通过类函数返回一个引用并在c ++中返回一个整个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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