Question

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. But the value of b does not change.

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?

Thank you in advance.

Was it helpful?

Solution 2

By assigning Ball b = a;, what it does is just to create a shallow copy of your object. So a and b have different allocated memory. Any change to properties in a (by value) has no effect to 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; then a.myX.r = 10; will also affect b.myX.r

This is because object copying is shallow. It doesn't copy what pointer pointing to, but only the pointer itself.

OTHER TIPS

No where are you "passing by reference". The code is working as expected. You create object a, and a new object b. Then you change r in object a. Because b is a whole separate object (unconnected to a), r does not change. It looks like you are trying to create a reference to the object a, in which you would do this:

Ball &b = a;

Now the code will print: b.GetR() : 10

By assigning b to a, you are invoking the implicit copy constructor for your Ball class:

Ball a;
Ball b;
b = a;

This makes b a copy of a. If you want to make b a reference to a, you should change the type to Ball&, like so:

Ball a;
Ball& b = a;

Ball a; Ball b;

b = a;//data members of object b will have a copy of data members of object 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. That's why you have that result.

If you want the change within object affect the other, you need to set: Ball &b = a;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top