简体   繁体   English

C ++类:通过引用传递

[英]C++ Class : Pass by reference

I am wondering what I am doing wrong here. 我想知道我在这里做错了什么。 I am creating a class a and a class b, setting b equal to a, setting a value of a, and printing that value of b. 我正在创建一个类a和一个类b,设置b等于a,设置a的值,并打印b的值。 But the value of b does not change. 但是b的价值并没有改变。

Here is my code: 这是我的代码:

#include <iostream>
using namespace std;

struct Node
{
  int x;
  int y;
};

class Ball
{
public:
  Ball();
  Ball(int r, int x, int y);

  int GetR();
  void SetR(int);

  int GetX();
  void SetX(int);

  int GetY();
  void SetY(int);

private:
  int m_r;
  int m_x;
  int m_y;
};

int main(int argc, char* argv[])
{
  Ball a;
  Ball b;

  b = a;

  a.SetR(10);

  cout << "a.GetR() : " << a.GetR() << endl;
  cout << "b.GetR() : " << b.GetR() << endl;

  return 0;
}

Ball::Ball()
{
  m_r = 0;
  m_x = 0;
  m_y = 0;
}

Ball::Ball(int r, int x, int y)
{
  m_r = r;
  m_x = x;
  m_y = y;
}

int Ball::GetR()
{
  return m_r;
}

void Ball::SetR(int r)
{
  m_r = r;
}

int Ball::GetX()
{
  return m_x;
}

void Ball::SetX(int x)
{
  m_x = x;
}

int Ball::GetY()
{
  return m_y;
}

void Ball::SetY(int y)
{
  m_y = y;
}

Here is my terminal output: 这是我的终端输出:

a.GetR() : 10
b.GetR() : 0

Can anyone explain to me the workings of class pass by reference in C++ and why this is happeneing? 任何人都可以向我解释一下C ++中类的传递方式以及为什么会发生这种情况?

Thank you in advance. 先感谢您。

No where are you "passing by reference". 不,你在哪里“通过引用传递”。 The code is working as expected. 代码按预期工作。 You create object a , and a new object b . 您创建对象a和新对象b Then you change r in object a . 然后你改变对象a r Because b is a whole separate object (unconnected to a ), r does not change. 因为b是一个完整的单独对象(未连接到a ),所以r不会改变。 It looks like you are trying to create a reference to the object a , in which you would do this: 看起来您正在尝试创建对象a引用 ,您可以在其中执行此操作:

Ball &b = a;

Now the code will print: b.GetR() : 10 现在代码将打印: b.GetR() : 10

By assigning Ball b = a; 通过分配Ball b = a; , what it does is just to create a shallow copy of your object. ,它的作用只是创建对象的浅表副本。 So a and b have different allocated memory. 所以a和b有不同的分配内存。 Any change to properties in a (by value) has no effect to b. a(按值)对属性的任何更改都不会对b产生影响。

The answer is you need to assign using reference: 答案是您需要使用引用分配:

Ball &b = a;

Just some little extra info, I'd like to explain more about the "shallow" word that I stressed in the above: 只是一些额外的信息,我想更多地解释一下我在上面强调的“浅薄”字:

class X {
   int r;
}

class A {
   X *myX;
}

class B {
   X *myB;
}

If I set A a; B b =a; 如果我设置A a; B b =a; A a; B b =a; then a.myX.r = 10; 那么a.myX.r = 10; will also affect b.myX.r 也会影响b.myX.r

This is because object copying is shallow. 这是因为对象复制很浅。 It doesn't copy what pointer pointing to, but only the pointer itself. 它不会复制指向的指针,而只复制指针本身。

By assigning b to a , you are invoking the implicit copy constructor for your Ball class: 通过将b分配给a ,您将调用Ball类的隐式复制构造函数:

Ball a;
Ball b;
b = a;

This makes b a copy of a . 这使得b副本a If you want to make b a reference to a , you should change the type to Ball& , like so: 如果你想b到参考a ,你应该改变类型Ball& ,就像这样:

Ball a;
Ball& b = a;

Ball a; 球a; Ball b; 球b;

b = a;//data members of object b will have a copy of data members of object a. b = a; //对象b的数据成员将具有对象a的数据成员的副本。 The data members from the two objects do connect together( or link or whatsoever). 来自两个对象的数据成员确实连接在一起(或链接或无论如何)。 They have their own values independently. 他们独立拥有自己的价值观。 When you change values of data members of one object, the change doesnt affect the data members of the other. 更改一个对象的数据成员的值时,更改不会影响另一个对象的数据成员。

a.SetR(10);// This changes data member of object a, not b. a.SetR(10); //这会更改对象a的数据成员,而不是b。 That's why you have that result. 这就是你有这个结果的原因。

If you want the change within object affect the other, you need to set: Ball &b = a; 如果您希望对象内的更改影响另一个,则需要设置:Ball&b = a;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM